-
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 14 Solutions #592
Merged
Merged
Changes from all commits
Commits
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,26 @@ | ||
// TC: O(n) | ||
// need to visit all nodes | ||
// SC: O(n) | ||
// normally O(log n) required however, O(n) in the worst case | ||
class Solution { | ||
private List<List<Integer>> output = new ArrayList<>(); | ||
public List<List<Integer>> levelOrder(TreeNode root) { | ||
dfs(0, root); | ||
return output; | ||
} | ||
|
||
private void dfs(int level, TreeNode node) { | ||
if (node == null) return; | ||
|
||
if (output.size() == level) { | ||
List<Integer> inside = new ArrayList<>(); | ||
inside.add(node.val); | ||
output.add(inside); | ||
} else { | ||
output.get(level).add(node.val); | ||
} | ||
level += 1; | ||
dfs(level, node.left); | ||
dfs(level, node.right); | ||
} | ||
} |
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,32 @@ | ||
// TC: O(n) | ||
// visit all nums at least once | ||
// SC: O(1) | ||
// only constant memory space required | ||
class Solution { | ||
public int rob(int[] nums) { | ||
if (nums.length == 1) return nums[0]; | ||
|
||
int prev = 0; | ||
int post = 0; | ||
int output1 = 0; | ||
|
||
for (int i = 0; i < nums.length - 1; i++) { | ||
int temp = prev; | ||
prev = Math.max(post + nums[i], prev); | ||
post = temp; | ||
} | ||
output1 = prev; | ||
|
||
prev = 0; | ||
post = 0; | ||
int output2 = 0; | ||
for (int i = 1; i < nums.length; i++) { | ||
int temp = prev; | ||
prev = Math.max(post + nums[i], prev); | ||
post = temp; | ||
} | ||
output2 = prev; | ||
|
||
return Math.max(output1, output2); | ||
} | ||
} |
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,22 @@ | ||
// TC: O(n) | ||
// visit all intervals to compare each of start time | ||
// SC: O(n) | ||
// PQ save all intervals in the worst case | ||
public class Solution { | ||
public int minMeetingRooms(List<Interval> intervals) { | ||
if (intervals == null || intervals.isEmpty()) return 0; | ||
|
||
intervals.sort((a, b) -> a.start - b.start); | ||
|
||
PriorityQueue<Integer> endTimes = new PriorityQueue<>(); | ||
endTimes.add(intervals.get(0).end); | ||
|
||
for (int i = 1; i < intervals.size(); i++) { | ||
Interval current = intervals.get(i); | ||
if (current.start >= endTimes.peek()) endTimes.poll(); | ||
endTimes.add(current.end); | ||
} | ||
|
||
return endTimes.size(); | ||
} | ||
} |
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,14 @@ | ||
public class Solution { | ||
public int reverseBits(int n) { | ||
// time complexity O(1) | ||
// space complexity O(1) | ||
int output = 0; | ||
|
||
for (int i = 0; i < Integer.SIZE; i++) { | ||
output <<= 1; | ||
output += n & 1; | ||
n >>= 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.
엇 time complexity가 O(N) 아닌가요?
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.
for문의 반복 횟수가
Integer.SIZE = 32
로 고정되니 O(1) 이라고 생각했는데 아닐까요!?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.
오 맞네요 제가 생각이 짧았습니다 ㅎㅎㅎ