-
Notifications
You must be signed in to change notification settings - Fork 126
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
62fca0d
Meeting Rooms
TonyKim9401 556a228
Lowest Common Ancestor Of A Binary Search Tree
TonyKim9401 4c325d2
House Robber
TonyKim9401 f277d32
Non Overlapping Intervals
TonyKim9401 68c9451
Lowest Common Ancestor Of A Binary Search Tree
TonyKim9401 0a0445b
Lowest Common Ancestor Of A Binary Search Tree
TonyKim9401 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
lowest-common-ancestor-of-a-binary-search-tree/TonyKim9401.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안녕하세요 토니님 :)
혹시 이 문제 푸신 걸까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@obzva 헐 다른 문제 풀었네요 세상엨ㅋㅋㅋㅋ
다시 풀어서 올리겠습니다 ㅠㅠㅠ..