Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5ca8ce3

Browse files
authoredNov 10, 2024
Merge pull request #578 from TonyKim9401/main
[Tony] WEEK 13 Solutions
2 parents 7b4cc6b + 0a0445b commit 5ca8ce3

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed
 

‎house-robber/TonyKim9401.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// TC: O(n)
2+
// always need to check every case
3+
// SC: O(n)
4+
// the length of the result int list is same with the length of the given nums int list
5+
class Solution {
6+
public int rob(int[] nums) {
7+
int[] result = new int[nums.length];
8+
9+
if (nums.length < 2) return nums[0];
10+
if (nums.length < 3) return Math.max(nums[0], nums[1]);
11+
12+
result[0] = nums[0];
13+
result[1] = Math.max(nums[0], nums[1]);
14+
15+
for (int i = 2; i < nums.length; i++) {
16+
result[i] = Math.max(result[i - 1], result[i - 2] + nums[i]);
17+
}
18+
19+
return result[nums.length-1];
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// TC: O(h)
2+
// h = the high of binary search tree
3+
// SC: O(1)
4+
// Doesn't require additional space
5+
class Solution {
6+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
7+
if (p.val < root.val && q.val < root.val)
8+
return lowestCommonAncestor(root.left, p, q);
9+
if (p.val > root.val && q.val > root.val)
10+
return lowestCommonAncestor(root.right, p, q);
11+
return root;
12+
}
13+
}

‎meeting-rooms/TonyKim9401.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TC: O(n log n)
2+
// list sort
3+
// SC: O(n)
4+
// list sort max use O(n)
5+
public class Solution {
6+
public boolean canAttendMeetings(List<Interval> intervals) {
7+
intervals.sort(Comparator.comparingInt(o -> o.start));
8+
9+
for (int i = 0; i < intervals.size() - 1; i++) {
10+
Interval preInterval = intervals.get(i);
11+
Interval postInterval = intervals.get(i+1);
12+
13+
if (preInterval.end > postInterval.start) return false;
14+
}
15+
return true;
16+
}
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// TC: O(n log n)
2+
// in order to order the given intervals array
3+
// SC: O(1)
4+
// only constant space necessary
5+
class Solution {
6+
public int eraseOverlapIntervals(int[][] intervals) {
7+
Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[1], o2[1]));
8+
9+
int output = 0;
10+
int end = intervals[0][1];
11+
12+
for (int i = 1; i < intervals.length; i++) {
13+
int[] currentInterval = intervals[i];
14+
15+
if (currentInterval[0] < end) output += 1;
16+
else end = currentInterval[1];
17+
}
18+
19+
return output;
20+
}
21+
}

0 commit comments

Comments
 (0)
Please sign in to comment.