Skip to content

[forest000014] Week 13 #1080

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 5 commits into from
Mar 8, 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
49 changes: 49 additions & 0 deletions find-median-from-data-stream/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
# Time Complexity: O(nlogn)
# Space Complexity: O(nlogn)

*/
class MedianFinder {

private PriorityQueue<Integer> pq1; // 1번째 ~ 가운데 원소 (max heap)
private PriorityQueue<Integer> pq2; // 가운데+1번째 ~ 마지막 원소 (min heap)

public MedianFinder() {
pq1 = new PriorityQueue<>(Collections.reverseOrder());
pq2 = new PriorityQueue<>();
}

public void addNum(int num) {

if (pq1.size() == pq2.size()) {
if (pq1.peek() == null || pq1.peek() >= num) {
pq1.add(num);
} else {
pq2.add(num);
pq1.add(pq2.poll());
}
} else {
if (pq1.peek() == null || pq1.peek() >= num) {
pq1.add(num);
pq2.add(pq1.poll());
} else {
pq2.add(num);
}
}
}

public double findMedian() {
if (pq1.size() == pq2.size()) {
return (pq1.peek() + pq2.peek()) / 2.0;
} else {
return pq1.peek();
}
}
}

/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder obj = new MedianFinder();
* obj.addNum(num);
* double param_2 = obj.findMedian();
*/
34 changes: 34 additions & 0 deletions insert-interval/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
# Time Complexity: O(n)
# Space Complexity: O(1)
*/
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
ArrayList<int[]> answer = new ArrayList<>();

int i = 0;
int n = intervals.length;

// newInterval보다 왼쪽의 구간들 추가
while (i < n && intervals[i][1] < newInterval[0]) {
answer.add(intervals[i]);
i++;
}

// newInterval과 겹치는 구간들 병합
while (i < n && intervals[i][0] <= newInterval[1]) {
newInterval[0] = Math.min(newInterval[0], intervals[i][0]);
newInterval[1] = Math.max(newInterval[1], intervals[i][1]);
i++;
}
answer.add(newInterval);

// newInterval보다 오른쪽의 구간들 추가
while (i < n) {
answer.add(intervals[i]);
i++;
}

return answer.toArray(new int[answer.size()][]);
}
}
53 changes: 53 additions & 0 deletions kth-smallest-element-in-a-bst/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
# Time Complexity: O(n)
# Space Complexity: O(n)

heap을 사용한 풀이, in-order traversal을 사용한 풀이 풀어볼 것!
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int kthSmallest(TreeNode root, int k) {
Map<TreeNode, Integer> sizes = new HashMap<>();

calculateSize(root, sizes);

return findKth(root, k, sizes);
}

private int calculateSize(TreeNode root, Map<TreeNode, Integer> sizes) {
if (root == null) return 0;

int left = calculateSize(root.left, sizes);
int right = calculateSize(root.right, sizes);

sizes.put(root, left + right + 1);
return left + right + 1;
}

private int findKth(TreeNode root, int k, Map<TreeNode, Integer> sizes) {
int left = (root.left == null) ? 0 : sizes.get(root.left);
int right = (root.right == null) ? 0 : sizes.get(root.right);

if (left == k - 1) {
return root.val;
} else if (left >= k) {
return findKth(root.left, k, sizes);
} else {
return findKth(root.right, k - left - 1, sizes);
}
}
}
28 changes: 28 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
# Time Complexity: O(n)
# Space Compleixty: O(1)
*/

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/

class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// null check unnecessary

if (root.val > p.val && root.val > q.val) {
return lowestCommonAncestor(root.left, p, q);
} else if (root.val < p.val && root.val < q.val) {
return lowestCommonAncestor(root.right, p, q);
} else {
return root;
}
}
}
15 changes: 15 additions & 0 deletions meeting-rooms/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
# Time Complexity: O(nlogn)
# Space Complexity: O(1)
*/
class Solution {
public boolean canAttendMeetings(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);

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

return true;
}
}