Skip to content

[sora0319] Week13 Solutions #1627

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 6 commits into from
Jul 2, 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
27 changes: 27 additions & 0 deletions find-median-from-data-stream/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class MedianFinder {
private PriorityQueue<Integer> maxHeap;
private PriorityQueue<Integer> minHeap;

public MedianFinder() {
maxHeap = new PriorityQueue<>(Collections.reverseOrder());
minHeap = new PriorityQueue<>();
}

public void addNum(int num) {
maxHeap.offer(num);
minHeap.offer(maxHeap.poll());

if (minHeap.size() > maxHeap.size()) {
maxHeap.offer(minHeap.poll());
}
}

public double findMedian() {
if (maxHeap.size() > minHeap.size()) {
return maxHeap.peek();
} else {
return (maxHeap.peek() + minHeap.peek()) / 2.0;
}
}
}

32 changes: 32 additions & 0 deletions insert-interval/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> list = new ArrayList<>();

int order = 0;
while (order < intervals.length && intervals[order][0] < newInterval[0]) {
list.add(intervals[order]);
order++;
}
list.add(newInterval);
while (order < intervals.length) {
list.add(intervals[order]);
order++;
}

List<int[]> output = new ArrayList<>();
output.add(list.get(0));

for (int i = 1; i < list.size(); i++) {
int[] last = output.get(output.size() - 1);
int[] current = list.get(i);
if (last[1] < current[0]) {
output.add(current);
} else {
last[1] = Math.max(last[1], current[1]);
}
}

return output.toArray(new int[output.size()][]);
}
}

21 changes: 21 additions & 0 deletions kth-smallest-element-in-a-bst/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Solution {
private int count = 0;
private int answer = -1;

public int kthSmallest(TreeNode root, int k) {
ordering(root, k);
return answer;
}

private void ordering(TreeNode node, int k) {
if (node == null) return;
ordering(node.left, k);
count++;
if (count == k) {
answer = node.val;
return;
}
ordering(node.right, k);
}
}

16 changes: 16 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public 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 (node.val < p.val && node.val < q.val) {
node = node.right;
} else {
return node;
}
}
return null;
}
}

14 changes: 14 additions & 0 deletions meeting-rooms/sora0319.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.*;

public class Solution {
public boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));

for (int i = 0; i < intervals.length - 1; i++) {
if (intervals[i][1] > intervals[i + 1][0]) {
return false;
}
}
return true;
}
}