-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Ackku] week 12 #1066
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
[Ackku] week 12 #1066
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 hidden or 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 @@ | ||
// 정렬을 하지 않으면 O(N^2)이 확정인 문제 | ||
// 정렬을 해서 O(NlogN)으로 해결 | ||
class Solution { | ||
public int eraseOverlapIntervals(int[][] intervals) { | ||
if (intervals.length == 0) return 0; | ||
|
||
Arrays.sort(intervals, (a, b) -> a[1] - b[1]); | ||
|
||
int count = 0; | ||
int prevEnd = intervals[0][1]; | ||
|
||
for (int i = 1; i < intervals.length; i++) { | ||
if (intervals[i][0] < prevEnd) { | ||
count++; | ||
} else { | ||
prevEnd = intervals[i][1]; | ||
} | ||
} | ||
return count; | ||
} | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
number-of-connected-components-in-an-undirected-graph/imsosleepy.java
This file contains hidden or 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,34 @@ | ||
// 그래프에서 연결된 컴포넌트 개수를 구하는 문제 | ||
// 인접 리스트로 변환, DFS로 연결을 확인한다 | ||
// 그래프 변환 → O(E) (간선 수), DFS/BFS 탐색 → O(V + E) (노드 + 간선) | ||
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[] visited = new boolean[n]; | ||
int count = 0; | ||
for (int i = 0; i < n; i++) { | ||
if (!visited[i]) { | ||
dfs(i, graph, visited); | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
private void dfs(int node, List<List<Integer>> graph, boolean[] visited) { | ||
if (visited[node]) return; | ||
visited[node] = true; | ||
for (int neighbor : graph.get(node)) { | ||
dfs(neighbor, graph, visited); | ||
} | ||
} | ||
} |
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. 풀이해 주신 대로 사이즈가 1일때 범용적으로 처리하는걸로 저도 많이 고민했었는데 자신만의 해답을 잘 찾아내신것 같아 너무 좋습니다 :) |
This file contains hidden or 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,25 @@ | ||
// 노드 전체를 한번만 탐색하게 만들었음 따라서 O(N) | ||
// 투포인터 방식으로 하나를 N+1 위치로 보내서 N번째 노드를 제거한 후 oneStep과 연결한다. | ||
// oneStep은 삭제할 노드 바로 직전 노드 | ||
// 사이즈가 1일 때를 범용적으로 처리하기 위해 머리를 굴렸음... | ||
class Solution { | ||
public ListNode removeNthFromEnd(ListNode head, int n) { | ||
ListNode defaultNode = new ListNode(0); | ||
defaultNode.next = head; | ||
ListNode twoStep = defaultNode; | ||
ListNode oneStep = defaultNode; | ||
|
||
for (int i = 0; i <= n; i++) { | ||
twoStep = twoStep.next; | ||
} | ||
|
||
while (twoStep != null) { | ||
twoStep = twoStep.next; | ||
oneStep = oneStep.next; | ||
} | ||
|
||
oneStep.next = oneStep.next.next; | ||
|
||
return defaultNode.next; | ||
} | ||
} |
This file contains hidden or 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,10 @@ | ||
class Solution { | ||
public boolean isSameTree(TreeNode p, TreeNode q) { | ||
// 끝까지 내려왔으면 같다. | ||
if (p == null && q == null) return true; | ||
// 끝까지 내려왔는데, 값이 다르다. | ||
if (p == null || q == null || p.val != q.val) return false; | ||
// 양쪽 다 탐색 | ||
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); | ||
} | ||
} |
This file contains hidden or 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,88 @@ | ||
// 문제 해석 부터 안되서 GPT에게 도움을 요청 | ||
// Serialize (직렬화): 트리를 문자열로 변환하는 과정. | ||
// Deserialize (역직렬화): 문자열을 다시 트리로 변환하는 과정. | ||
// 트리를 저장하고 복원할 수 있는 형식이라면 어떤 방법이든 가능. | ||
// 🔹 해결 방법 | ||
// 우리는 BFS(너비 우선 탐색)와 큐(Queue)를 활용한 방식을 사용할 거야. | ||
// 이 방식을 선택한 이유는: | ||
|
||
// 트리의 구조를 유지하면서도 직렬화하기 쉽다. | ||
// 문자열이 순차적으로 만들어져 역직렬화할 때도 다시 순차적으로 트리를 복원하기 편하다. | ||
public class Codec { | ||
|
||
// 직렬화 (Serialize) | ||
public String serialize(TreeNode root) { | ||
if (root == null) return "null"; // 빈 트리 처리 | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
Queue<TreeNode> queue = new LinkedList<>(); | ||
queue.offer(root); | ||
|
||
while (!queue.isEmpty()) { | ||
TreeNode node = queue.poll(); | ||
|
||
if (node == null) { | ||
sb.append("null,"); | ||
} else { | ||
sb.append(node.val).append(","); | ||
queue.offer(node.left); | ||
queue.offer(node.right); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
// 역직렬화 (Deserialize) | ||
public TreeNode deserialize(String data) { | ||
if (data.equals("null")) return null; // 빈 트리 처리 | ||
|
||
String[] values = data.split(","); | ||
TreeNode root = new TreeNode(Integer.parseInt(values[0])); | ||
Queue<TreeNode> queue = new LinkedList<>(); | ||
queue.offer(root); | ||
|
||
int i = 1; | ||
while (!queue.isEmpty()) { | ||
TreeNode node = queue.poll(); | ||
|
||
if (!values[i].equals("null")) { | ||
node.left = new TreeNode(Integer.parseInt(values[i])); | ||
queue.offer(node.left); | ||
} | ||
i++; | ||
|
||
if (!values[i].equals("null")) { | ||
node.right = new TreeNode(Integer.parseInt(values[i])); | ||
queue.offer(node.right); | ||
} | ||
i++; | ||
} | ||
return root; | ||
} | ||
} | ||
|
||
// 트리는 대부분 DFS로 해결했어서 비슷한 방식을 생각했으나 구현 실패 | ||
// GPT에게 이어서 작업을 했고 O(N)의 시간복잡도를 얻음. 그러나 위의 BFS보다 속도가 느림 | ||
class Codec { | ||
// 🔹 DFS 기반 직렬화 (Serialize) | ||
public String serialize(TreeNode root) { | ||
if (root == null) return "null"; | ||
return root.val + "," + serialize(root.left) + "," + serialize(root.right); | ||
} | ||
|
||
// 🔹 DFS 기반 역직렬화 (Deserialize) | ||
public TreeNode deserialize(String data) { | ||
Queue<String> nodes = new LinkedList<>(Arrays.asList(data.split(","))); | ||
return buildTree(nodes); | ||
} | ||
|
||
private TreeNode buildTree(Queue<String> nodes) { | ||
String val = nodes.poll(); | ||
if (val.equals("null")) return null; | ||
|
||
TreeNode node = new TreeNode(Integer.parseInt(val)); | ||
node.left = buildTree(nodes); | ||
node.right = buildTree(nodes); | ||
return node; | ||
} | ||
} |
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.
시간 복잡도 접근이 너무 좋습니다 :)