Skip to content

[minji-go] week 13 solutions #1619

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 4 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
45 changes: 45 additions & 0 deletions find-median-from-data-stream/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* <a href="https://leetcode.com/problems/find-median-from-data-stream/">week13-4. find-median-from-data-stream</a>
* <li>Description: Implement the MedianFinder class</li>
* <li>Topics: Two Pointers, Design, Sorting, Heap (Priority Queue), Data Stream</li>
* <li>Time Complexity: O(logN), Runtime 99ms </li>
* <li>Space Complexity: O(N), Memory 63.68MB </li>
*/

class MedianFinder {
PriorityQueue<Integer> head;
PriorityQueue<Integer> tail;

public MedianFinder() {
head = new PriorityQueue<>(Comparator.reverseOrder());
tail = new PriorityQueue<>();
}

public void addNum(int num) {
if(head.isEmpty() || num <= head.peek()) {
head.add(num);
} else {
tail.add(num);
}

if (head.size() > tail.size() + 1) {
tail.add(head.poll());
} else if (head.size() < tail.size()) {
head.add(tail.poll());
}
}

public double findMedian() {
if(head.size() == tail.size()){
return ((double)head.peek() + tail.peek()) / 2;
}
return head.peek();
}
}

/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/
30 changes: 30 additions & 0 deletions insert-interval/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* <a href="https://leetcode.com/problems/insert-interval/">week13-3. insert-interval</a>
* <li>Description: Return intervals after the insertion of 'new interval' given an array of non-overlapping intervals</li>
* <li>Topics: Array </li>
* <li>Time Complexity: O(N), Runtime 1ms </li>
* <li>Space Complexity: O(N), Memory 45.02MB </li>
*/
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> answer = new ArrayList<>();

int i = 0;
while (i < intervals.length && intervals[i][1] < newInterval[0]) {
answer.add(intervals[i++]);
}

while (i < intervals.length && newInterval[1] >= intervals[i][0]) {
newInterval[0] = Math.min(intervals[i][0], newInterval[0]);
newInterval[1] = Math.max(intervals[i][1], newInterval[1]);
i++;
}
answer.add(newInterval);

while (i < intervals.length) {
answer.add(intervals[i++]);
}

return answer.toArray(new int[answer.size()][]);
}
}
31 changes: 31 additions & 0 deletions kth-smallest-element-in-a-bst/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* <a href="https://leetcode.com/problems/kth-smallest-element-in-a-bst/">week13-3. kth-smallest-element-in-a-bst</a>
* <li>Description: Given the root of a binary search tree, and an integer k, return the kth smallest value</li>
* <li>Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree</li>
* <li>Time Complexity: O(N), Runtime 0ms </li>
* <li>Space Complexity: O(H), Memory 44.5MB</li>
* <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>
*/
class Solution {
public int kthSmallest(TreeNode root, int k) {
return inorder(root, new AtomicInteger(k));
}

public Integer inorder(TreeNode node, AtomicInteger k) {
if (node == null) {
return null;
}

Integer value = inorder(node.left, k);
if (value != null) {
return value;
}

if (k.decrementAndGet() == 0) {
return node.val;
}

return inorder(node.right, k);
}

}
24 changes: 24 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* <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>
* <li>Description: find the lowest common ancestor (LCA) node of two given nodes in the BST. </li>
* <li>Topics: Tree, Depth-First Search, Binary Search Tree, Binary Tree </li>
* <li>Time Complexity: O(H), Runtime 5ms </li>
* <li>Space Complexity: O(1), Memory 44.91MB </li>
*/

class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode node = root;
while (node != null) {
if (p.val < node.val && q.val < node.val) {
node = node.left;
} else if (p.val > node.val && q.val > node.val) {
node = node.right;
} else {
return node;
}
}

throw new IllegalArgumentException();
}
}