Skip to content

Commit eb04063

Browse files
authored
Merge pull request #1619 from minji-go/week13
[minji-go] week 13 solutions
2 parents d9c99ae + 9202c79 commit eb04063

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/find-median-from-data-stream/">week13-4. find-median-from-data-stream</a>
3+
* <li>Description: Implement the MedianFinder class</li>
4+
* <li>Topics: Two Pointers, Design, Sorting, Heap (Priority Queue), Data Stream</li>
5+
* <li>Time Complexity: O(logN), Runtime 99ms </li>
6+
* <li>Space Complexity: O(N), Memory 63.68MB </li>
7+
*/
8+
9+
class MedianFinder {
10+
PriorityQueue<Integer> head;
11+
PriorityQueue<Integer> tail;
12+
13+
public MedianFinder() {
14+
head = new PriorityQueue<>(Comparator.reverseOrder());
15+
tail = new PriorityQueue<>();
16+
}
17+
18+
public void addNum(int num) {
19+
if(head.isEmpty() || num <= head.peek()) {
20+
head.add(num);
21+
} else {
22+
tail.add(num);
23+
}
24+
25+
if (head.size() > tail.size() + 1) {
26+
tail.add(head.poll());
27+
} else if (head.size() < tail.size()) {
28+
head.add(tail.poll());
29+
}
30+
}
31+
32+
public double findMedian() {
33+
if(head.size() == tail.size()){
34+
return ((double)head.peek() + tail.peek()) / 2;
35+
}
36+
return head.peek();
37+
}
38+
}
39+
40+
/**
41+
* Your MedianFinder object will be instantiated and called as such:
42+
* MedianFinder obj = new MedianFinder();
43+
* obj.addNum(num);
44+
* double param_2 = obj.findMedian();
45+
*/

insert-interval/minji-go.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/insert-interval/">week13-3. insert-interval</a>
3+
* <li>Description: Return intervals after the insertion of 'new interval' given an array of non-overlapping intervals</li>
4+
* <li>Topics: Array </li>
5+
* <li>Time Complexity: O(N), Runtime 1ms </li>
6+
* <li>Space Complexity: O(N), Memory 45.02MB </li>
7+
*/
8+
class Solution {
9+
public int[][] insert(int[][] intervals, int[] newInterval) {
10+
List<int[]> answer = new ArrayList<>();
11+
12+
int i = 0;
13+
while (i < intervals.length && intervals[i][1] < newInterval[0]) {
14+
answer.add(intervals[i++]);
15+
}
16+
17+
while (i < intervals.length && newInterval[1] >= intervals[i][0]) {
18+
newInterval[0] = Math.min(intervals[i][0], newInterval[0]);
19+
newInterval[1] = Math.max(intervals[i][1], newInterval[1]);
20+
i++;
21+
}
22+
answer.add(newInterval);
23+
24+
while (i < intervals.length) {
25+
answer.add(intervals[i++]);
26+
}
27+
28+
return answer.toArray(new int[answer.size()][]);
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/kth-smallest-element-in-a-bst/">week13-3. kth-smallest-element-in-a-bst</a>
3+
* <li>Description: Given the root of a binary search tree, and an integer k, return the kth smallest value</li>
4+
* <li>Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree</li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(H), Memory 44.5MB</li>
7+
* <li>Note: If the BST is modified often (with frequent insertions or deletions), consider using an Augmented BST, Segment Tree, or TreeMap with additional metadata to efficiently support dynamic updates and k-th smallest queries in logarithmic time. </li>
8+
*/
9+
class Solution {
10+
public int kthSmallest(TreeNode root, int k) {
11+
return inorder(root, new AtomicInteger(k));
12+
}
13+
14+
public Integer inorder(TreeNode node, AtomicInteger k) {
15+
if (node == null) {
16+
return null;
17+
}
18+
19+
Integer value = inorder(node.left, k);
20+
if (value != null) {
21+
return value;
22+
}
23+
24+
if (k.decrementAndGet() == 0) {
25+
return node.val;
26+
}
27+
28+
return inorder(node.right, k);
29+
}
30+
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/">week13-2. lowest-common-ancestor-of-a-binary-search-tree</a>
3+
* <li>Description: find the lowest common ancestor (LCA) node of two given nodes in the BST. </li>
4+
* <li>Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree </li>
5+
* <li>Time Complexity: O(H), Runtime 5ms </li>
6+
* <li>Space Complexity: O(1), Memory 44.91MB </li>
7+
*/
8+
9+
class Solution {
10+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
11+
TreeNode node = root;
12+
while (node != null) {
13+
if (p.val < node.val && q.val < node.val) {
14+
node = node.left;
15+
} else if (p.val > node.val && q.val > node.val) {
16+
node = node.right;
17+
} else {
18+
return node;
19+
}
20+
}
21+
22+
throw new IllegalArgumentException();
23+
}
24+
}

0 commit comments

Comments
 (0)