diff --git a/find-median-from-data-stream/PDKhan.cpp b/find-median-from-data-stream/PDKhan.cpp new file mode 100644 index 000000000..76eac9b5b --- /dev/null +++ b/find-median-from-data-stream/PDKhan.cpp @@ -0,0 +1,31 @@ +class MedianFinder { + priority_queue maxHeap; + priority_queue, greater> 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; + } +}; diff --git a/insert-interval/PDKhan.cpp b/insert-interval/PDKhan.cpp new file mode 100644 index 000000000..9ab139907 --- /dev/null +++ b/insert-interval/PDKhan.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + vector> insert(vector>& intervals, vector& newInterval) { + vector> 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; + } +}; diff --git a/kth-smallest-element-in-a-bst/PDKhan.cpp b/kth-smallest-element-in-a-bst/PDKhan.cpp new file mode 100644 index 000000000..7c54db44d --- /dev/null +++ b/kth-smallest-element-in-a-bst/PDKhan.cpp @@ -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; + } +}; diff --git a/lowest-common-ancestor-of-a-binary-search-tree/PDKhan.cpp b/lowest-common-ancestor-of-a-binary-search-tree/PDKhan.cpp new file mode 100644 index 000000000..a38f429af --- /dev/null +++ b/lowest-common-ancestor-of-a-binary-search-tree/PDKhan.cpp @@ -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; + } +}; diff --git a/meeting-rooms/PDKhan.cpp b/meeting-rooms/PDKhan.cpp new file mode 100644 index 000000000..d251572fb --- /dev/null +++ b/meeting-rooms/PDKhan.cpp @@ -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 &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; + } +};