-
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 12 Solutions #564
Changes from all commits
5f1ed68
612595a
79fbf03
a14bddf
eda7ce0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// TC: O(n log n) | ||
// It takes n log n to sort array, visit all elements in O(n) time, total O(n log n) | ||
// SC: O(n) | ||
// Needs max O(n) space to save intervals | ||
class Solution { | ||
public int[][] merge(int[][] intervals) { | ||
int n = intervals.length; | ||
if (n == 1) return intervals; | ||
|
||
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); | ||
List<int[]> output = new ArrayList<>(); | ||
output.add(intervals[0]); | ||
|
||
int[] currentInterval = intervals[0]; | ||
|
||
for (int[] interval : intervals) { | ||
int currentEnd = currentInterval[1]; | ||
int nextStart = interval[0]; | ||
int nextEnd = interval[1]; | ||
if (currentEnd >= nextStart) { | ||
currentInterval[1] = Math.max(currentEnd, nextEnd); | ||
} else { | ||
currentInterval = interval; | ||
output.add(currentInterval); | ||
} | ||
} | ||
|
||
return output.toArray(new int[output.size()][]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// TC: O(n + m) | ||
// n = the number of nodes, m = the number of edges | ||
// SC: O(n + m) | ||
// n, m are the each size of the 2 demension array 'edges' | ||
public class Solution { | ||
public int countComponents(int n, int[][] edges) { | ||
List<List<Integer>> graph = new ArrayList<>(); | ||
|
||
for (int i = 0; i < n; i++) graph.add(new ArrayList<>()); | ||
|
||
for (int[] edge : edges) { | ||
graph.get(edge[0]).add(edge[1]); | ||
graph.get(edge[1]).add(edge[0]); | ||
} | ||
|
||
boolean[] visit = new boolean[n]; | ||
int count = 0; | ||
|
||
for (int i = 0; i < n; i++) { | ||
if (!visit[i]) { | ||
count += 1; | ||
dfs(i, graph, visit); | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
private void dfs(int node, List<List<Integer>> graph, boolean[] visit) { | ||
visit[node] = true; | ||
for (int neighbor : graph.get(node)) { | ||
if (!visit[neighbor]) dfs(neighbor, graph, visit); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// TC: O(n) | ||
// Visit all elements in the worst case | ||
// SC: O(1) | ||
// Keep using ready assigned variables only | ||
class Solution { | ||
public ListNode removeNthFromEnd(ListNode head, int n) { | ||
ListNode output = new ListNode(0, head); | ||
ListNode dummy = output; | ||
|
||
for (int i = 0; i < n; i++) head = head.next; | ||
|
||
while (head != null) { | ||
head = head.next; | ||
dummy = dummy.next; | ||
} | ||
|
||
dummy.next = dummy.next.next; | ||
|
||
return output.next; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,17 @@ | ||||||||
// TC: O(n) | ||||||||
// retreive all given nodes | ||||||||
// 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. dfs 스택을 사용하는 만큼 |
||||||||
// doesn't require additional space | ||||||||
class Solution { | ||||||||
public boolean isSameTree(TreeNode p, TreeNode q) { | ||||||||
if (p == null || q == null) { | ||||||||
if (p == null && q == null) return true; | ||||||||
return false; | ||||||||
Comment on lines
+8
to
+9
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. 😉
Suggested change
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. 헐 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. L7 ~ L10 을 조금 더 줄여서 아래처럼 가능할 것 같습니다! if (p == null || q == null) {
return p == q;
} |
||||||||
} | ||||||||
|
||||||||
if (p.val != q.val) return false; | ||||||||
|
||||||||
return isSameTree(p.left, q.left) && | ||||||||
isSameTree(p.right, q.right); | ||||||||
} | ||||||||
} |
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.
(사소) 이 조건이면 max 로 하지 않고,currentInterval[1] = currentEnd
로 해도 괜찮아 보입니다!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.
아앗
nextStart
와nextEnd
를 잘못 봤군요 위 내용은 넘어가시면 될 것 같습니다!