Skip to content
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

[Tony] WEEK 13 Solutions #578

Merged
merged 6 commits into from
Nov 10, 2024
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
21 changes: 21 additions & 0 deletions house-robber/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// TC: O(n)
// always need to check every case
// SC: O(n)
// the length of the result int list is same with the length of the given nums int list
class Solution {
public int rob(int[] nums) {
int[] result = new int[nums.length];

if (nums.length < 2) return nums[0];
if (nums.length < 3) return Math.max(nums[0], nums[1]);

result[0] = nums[0];
result[1] = Math.max(nums[0], nums[1]);

for (int i = 2; i < nums.length; i++) {
result[i] = Math.max(result[i - 1], result[i - 2] + nums[i]);
}

return result[nums.length-1];
}
}
13 changes: 13 additions & 0 deletions lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요 토니님 :)
혹시 이 문제 푸신 걸까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@obzva 헐 다른 문제 풀었네요 세상엨ㅋㅋㅋㅋ
다시 풀어서 올리겠습니다 ㅠㅠㅠ..

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// TC: O(h)
// h = the high of binary search tree
// SC: O(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

스택 메모리를 고려해야 하지 않을까요~?

// Doesn't require additional space
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (p.val < root.val && q.val < root.val)
return lowestCommonAncestor(root.left, p, q);
if (p.val > root.val && q.val > root.val)
return lowestCommonAncestor(root.right, p, q);
return root;
}
}
17 changes: 17 additions & 0 deletions meeting-rooms/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// TC: O(n log n)
// list sort
// SC: O(n)
// list sort max use O(n)
public class Solution {
public boolean canAttendMeetings(List<Interval> intervals) {
intervals.sort(Comparator.comparingInt(o -> o.start));

for (int i = 0; i < intervals.size() - 1; i++) {
Interval preInterval = intervals.get(i);
Interval postInterval = intervals.get(i+1);

if (preInterval.end > postInterval.start) return false;
}
return true;
}
}
21 changes: 21 additions & 0 deletions non-overlapping-intervals/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// TC: O(n log n)
// in order to order the given intervals array
// SC: O(1)
// only constant space necessary
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {
Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[1], o2[1]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparator 를 다양하게 구현해주셔서 보는 입장에서도 한번 더 살펴볼 수 있어 좋은 것 같습니다!


int output = 0;
int end = intervals[0][1];

for (int i = 1; i < intervals.length; i++) {
int[] currentInterval = intervals[i];

if (currentInterval[0] < end) output += 1;
else end = currentInterval[1];
}

return output;
}
}