Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
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();
}
}