-
-
Notifications
You must be signed in to change notification settings - Fork 229
[sora0349] Week 12 Solutions #1606
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
Changes from all commits
0ba3e14
8904e80
47fcaf5
f8876eb
b4737e0
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,20 @@ | ||
public class Solution { | ||
public int eraseOverlapIntervals(int[][] intervals) { | ||
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0])); | ||
int count = 0; | ||
|
||
int prv_end = intervals[0][1]; | ||
for (int i = 1; i < intervals.length; i++) { | ||
int start = intervals[i][0]; | ||
int end = intervals[i][1]; | ||
if (prv_end > start) { | ||
count++; | ||
prv_end = Math.min(end, prv_end); | ||
} else { | ||
prv_end = end; | ||
} | ||
} | ||
return count; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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) { | ||
int node = edge[0]; | ||
int adj = edge[1]; | ||
graph.get(node).add(adj); | ||
graph.get(adj).add(node); | ||
} | ||
|
||
int count = 0; | ||
Set<Integer> visited = new HashSet<>(); | ||
for (int node = 0; node < n; node++) { | ||
if (visited.contains(node)) { | ||
continue; | ||
} | ||
count++; | ||
Deque<Integer> queue = new ArrayDeque<>(); | ||
queue.push(node); | ||
while (!queue.isEmpty()) { | ||
int cur = queue.pop(); | ||
if (visited.contains(cur)) continue; | ||
visited.add(cur); | ||
for (int g : graph.get(cur)) { | ||
if (!visited.contains(g)) { | ||
queue.push(g); | ||
} | ||
} | ||
} | ||
} | ||
return count; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
public class Solution { | ||
public ListNode removeNthFromEnd(ListNode head, int n) { | ||
Queue<ListNode> queue = new LinkedList<>(); | ||
ListNode temp = new ListNode(0, head); | ||
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. 공간 복잡도에 대해서는 생각하지 못했는데 투 포인터 방식으로 해볼 수 있겠네요! |
||
ListNode node = temp; | ||
|
||
for (int i = 0; i < n + 1; i++) { | ||
queue.offer(node); | ||
node = node.next; | ||
} | ||
|
||
while (node != null) { | ||
queue.poll(); | ||
queue.offer(node); | ||
node = node.next; | ||
} | ||
|
||
ListNode prev = queue.poll(); | ||
prev.next = prev.next.next; | ||
|
||
return temp.next; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
public class Solution { | ||
public boolean isSameTree(TreeNode p, TreeNode q) { | ||
Stack<TreeNode[]> stack = new Stack<>(); | ||
stack.push(new TreeNode[]{p, q}); | ||
|
||
while (!stack.isEmpty()) { | ||
TreeNode[] nodes = stack.pop(); | ||
TreeNode n1 = nodes[0]; | ||
TreeNode n2 = nodes[1]; | ||
|
||
if (n1 == null && n2 == null) { | ||
continue; | ||
} | ||
if (n1 == null || n2 == null) { | ||
return false; | ||
} | ||
if (n1.val != n2.val) { | ||
return false; | ||
} | ||
stack.push(new TreeNode[]{n1.left, n2.left}); | ||
stack.push(new TreeNode[]{n1.right, n2.right}); | ||
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. 스택을 활용하면 이렇게 풀이할 수 있군요!! 저는 재귀로만 풀어보았는데, 참고가 되었습니다 😄 |
||
} | ||
return true; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
public class Codec { | ||
public String serialize(TreeNode root) { | ||
if (root == null) { | ||
return "N"; | ||
} | ||
String left = serialize(root.left); | ||
String right = serialize(root.right); | ||
return root.val + "," + left + "," + right; | ||
} | ||
|
||
public TreeNode deserialize(String data) { | ||
Deque<String> values = new ArrayDeque<>(Arrays.asList(data.split(","))); | ||
return dfs(values); | ||
} | ||
|
||
private TreeNode dfs(Deque<String> values) { | ||
String value = values.pollFirst(); | ||
if (value.equals("N")) { | ||
return null; | ||
} | ||
TreeNode node = new TreeNode(Integer.parseInt(value)); | ||
node.left = dfs(values); | ||
node.right = dfs(values); | ||
return node; | ||
} | ||
} | ||
|
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.
intervals
를 start를 기준으로 오름차순 정렬한 후, 그리디하게 항상Math.min()
으로 더 작은 end를 선택하는 방식으로 풀이하신 것 같네요!intervals
를 end를 기준으로 오름차순 정렬하신다면prv_end
가 항상end
보다 같거나 작을 것이기 때문에Math.min()
로직을 제거할 수 있을 것 같습니다~!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.
의견 주셔서 감사합니다
한번 더 생각해 봐야 할 것 같네요