Skip to content

[PDKhan] WEEK 13 solutions #1620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions find-median-from-data-stream/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class MedianFinder {
priority_queue<int> maxHeap;
priority_queue<int, vector<int>, greater<int>> minHeap;
public:
MedianFinder() {
}

void addNum(int num) {
if(maxHeap.empty() || num <= maxHeap.top())
maxHeap.push(num);
else
minHeap.push(num);

if(maxHeap.size() > minHeap.size() + 1){
minHeap.push(maxHeap.top());
maxHeap.pop();
}else if(maxHeap.size() < minHeap.size()){
maxHeap.push(minHeap.top());
minHeap.pop();
}
}

double findMedian() {
if(maxHeap.size() > minHeap.size())
return maxHeap.top();
else if(maxHeap.size() < minHeap.size())
return minHeap.top();
else
return (maxHeap.top() + minHeap.top()) / 2.0;
}
};
28 changes: 28 additions & 0 deletions insert-interval/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
vector<vector<int>> result;
int i = 0;
int n = intervals.size();

while(i < n && intervals[i][1] < newInterval[0]){
result.push_back(intervals[i]);
i++;
}

while(i < n && intervals[i][0] <= newInterval[1]){
newInterval[0] = min(newInterval[0], intervals[i][0]);
newInterval[1] = max(newInterval[1], intervals[i][1]);
i++;
}

result.push_back(newInterval);

while(i < n){
result.push_back(intervals[i]);
i++;
}

return result;
}
};
27 changes: 27 additions & 0 deletions kth-smallest-element-in-a-bst/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
void search(TreeNode* root, int k, int& cnt, int& result){
if(root == NULL)
return;
if(cnt > k)
return;

search(root->left, k, cnt, result);

cnt++;

if(cnt == k)
result = root->val;

search(root->right, k, cnt, result);
}

int kthSmallest(TreeNode* root, int k) {
int cnt = 0;
int result = 0;

search(root, k, cnt, result);

return result;
}
};
11 changes: 11 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root->val < p->val && root->val < q->val)
return lowestCommonAncestor(root->right, p, q);
else if(root->val > p->val && root->val > q->val)
return lowestCommonAncestor(root->left, p, q);
else
return root;
}
};
20 changes: 20 additions & 0 deletions meeting-rooms/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
/**
* @param intervals: an array of meeting time intervals
* @return: if a person could attend all meetings
*/
bool canAttendMeetings(vector<Interval> &intervals) {
// Write your code here
sort(intervals.begin(), intervals.end(), [](const Interval& a, const Interval& b){
return a.start < b.start;
});

for(int i = 1; i < intervals.size(); i++){
if(intervals[i].start < intervals[i - 1].end)
return false;
}

return true;
}
};