Skip to content

[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

Merged
merged 5 commits into from
Jun 22, 2025
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
20 changes: 20 additions & 0 deletions non-overlapping-intervals/sora0319.java
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);
Copy link
Contributor

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를 선택하는 방식으로 풀이하신 것 같네요!

intervalsend를 기준으로 오름차순 정렬하신다면 prv_end가 항상 end 보다 같거나 작을 것이기 때문에 Math.min() 로직을 제거할 수 있을 것 같습니다~!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

의견 주셔서 감사합니다
한번 더 생각해 봐야 할 것 같네요

} 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;
}
}

24 changes: 24 additions & 0 deletions remove-nth-node-from-end-of-list/sora0319.java
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

temp 노드를 이용함으로써 head가 삭제되는 케이스를 커버할 수 있는 좋은 풀이 방법인 것 같아요 🤩

다만, queue를 고정된 크기로 유지하며 O(n)의 공간을 사용하게 되는 부분을 투 포인터를 이용하여 슬라이딩 윈도우로 다룬다면 O(1)의 공간으로 최적화가 가능할 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}
}

26 changes: 26 additions & 0 deletions same-tree/sora0319.java
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});
Copy link
Contributor

Choose a reason for hiding this comment

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

스택을 활용하면 이렇게 풀이할 수 있군요!! 저는 재귀로만 풀어보았는데, 참고가 되었습니다 😄

}
return true;
}
}

27 changes: 27 additions & 0 deletions serialize-and-deserialize-binary-tree/sora0319.java
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;
}
}