-
-
Notifications
You must be signed in to change notification settings - Fork 336
[juhui-jeong] WEEK 12 Solutions #2606
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
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 |
|---|---|---|
| @@ -1,51 +1,79 @@ | ||
| /* | ||
| * 끝나는 시간 오름차순 정렬 | ||
| */ | ||
| 처음 풀이 | ||
| 시작 시간 기준 정렬 | ||
| class Solution { | ||
| public int eraseOverlapIntervals(int[][] intervals) { | ||
| Arrays.sort(intervals, (a,b) -> Integer.compare(a[1], b[1])); | ||
| Arrays.sort(intervals, (a,b) -> { | ||
| if (a[0] != b[0]) return Integer.compare(a[0], b[0]); | ||
| return Integer.compare(a[1], b[1]); | ||
| }); | ||
|
|
||
| int removeCntResult = 0; | ||
| int end = intervals[0][1]; | ||
| int[] cur = intervals[0]; | ||
|
|
||
| for (int i = 1; i< intervals.length; i++) { | ||
| if (intervals[i][0] < end) { | ||
| int [] next = intervals[i]; | ||
|
|
||
| if (cur[1] > next[0]) { | ||
| removeCntResult += 1; | ||
| if (next[1] < cur[1]) { | ||
| cur = next; | ||
| } | ||
| } else { | ||
| end = intervals[i][1]; | ||
| cur = next; | ||
| } | ||
| } | ||
| return removeCntResult; | ||
| } | ||
| } | ||
| */ | ||
|
|
||
|
|
||
| /* | ||
| 처음 풀이 | ||
| 시작 시간 기준 정렬 | ||
| * 끝나는 시간 오름차순 정렬 | ||
|
|
||
| class Solution { | ||
| public int eraseOverlapIntervals(int[][] intervals) { | ||
| Arrays.sort(intervals, (a,b) -> { | ||
| if (a[0] != b[0]) return Integer.compare(a[0], b[0]); | ||
| return Integer.compare(a[1], b[1]); | ||
| }); | ||
| Arrays.sort(intervals, (a,b) -> Integer.compare(a[1], b[1])); | ||
|
|
||
| int removeCntResult = 0; | ||
| int[] cur = intervals[0]; | ||
| int end = intervals[0][1]; | ||
|
|
||
| for (int i = 1; i< intervals.length; i++) { | ||
| int [] next = intervals[i]; | ||
|
|
||
| if (cur[1] > next[0]) { | ||
| if (intervals[i][0] < end) { | ||
| removeCntResult += 1; | ||
| if (next[1] < cur[1]) { | ||
| cur = next; | ||
| } | ||
| } else { | ||
| cur = next; | ||
| end = intervals[i][1]; | ||
| } | ||
| } | ||
| return removeCntResult; | ||
| } | ||
| } | ||
| */ | ||
|
|
||
| /** | ||
| * 시간 복잡도: O(n log n) | ||
| * 공간 복잡도: O(n) | ||
| */ | ||
| class Solution { | ||
| public int eraseOverlapIntervals(int[][] intervals) { | ||
| Arrays.sort(intervals, (a,b) -> a[0] - b[0]); | ||
| int count = 0; | ||
| int prevEnd = intervals[0][1]; | ||
|
|
||
| for (int i = 1; i < intervals.length; i++) { | ||
| int curStart = intervals[i][0]; | ||
| int curEnd = intervals[i][1]; | ||
|
|
||
| if (prevEnd <= curStart) { | ||
| // 안겹치는 경우(변경 없이 다음 배열로) | ||
| prevEnd = curEnd; | ||
| } else { | ||
| // 겹치는 경우 | ||
| count++; | ||
| prevEnd = Math.min(prevEnd, curEnd); | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
| } |
|
Contributor
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:
|
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(1) |
피드백: 빠른 포인터를 n만큼 이동시킨 후, 느린 포인터와 함께 이동하며 끝에서 n번째 노드를 찾습니다. 리스트 길이만큼 한 번 순회하므로 시간 복잡도는 O(n), 추가 공간은 상수입니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.removeNthFromEnd — Time: O(n) / Space: O(1)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(1) |
피드백: 리스트 길이를 먼저 계산하고, 그 후 제거 위치를 찾기 위해 한 번 더 순회합니다. 두 번 순회지만 시간 복잡도는 O(n)이고, 공간은 상수입니다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
|
Contributor
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:
|
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(n) | O(n) | ✅ |
| Space | O(h) | O(h) | ✅ |
피드백: 모든 노드를 한 번씩 방문하므로 시간 복잡도는 O(n). 재귀 호출로 인해 호출 스택이 트리 높이만큼 쌓이므로 공간 복잡도는 O(h) (h는 트리의 높이)입니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Solution.isSameTree — Time: ✅ O(n) → O(n) / Space: ✅ O(n) → O(n)
| 유저 분석 | 실제 분석 | 결과 | |
|---|---|---|---|
| Time | O(n) | O(n) | ✅ |
| Space | O(n) | O(n) | ✅ |
피드백: 모든 노드를 한 번씩 방문하므로 시간 복잡도는 O(n). 큐를 사용하는데, 최악의 경우 모든 노드가 큐에 저장되므로 공간 복잡도는 O(n)입니다.
개선 제안: 현재 구현이 적절해 보입니다.
|
Contributor
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:
|
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 모든 노드를 한 번씩 방문하여 문자열로 변환하므로 시간 복잡도는 O(n). 재귀 호출로 인한 호출 스택과 결과 문자열 크기 때문에 공간 복잡도도 O(n)입니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Codec.deserialize — Time: O(n) / Space: O(n)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | O(n) |
피드백: 입력 문자열을 배열로 분할 후, 재귀적으로 노드를 재구성하므로 시간 복잡도는 O(n). 배열 크기만큼 호출 스택이 쌓이므로 공간 복잡도도 O(n)입니다.
개선 제안: 현재 구현이 적절해 보입니다.
💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
|
|
||
| public class Codec { | ||
| private int index; | ||
|
|
||
| // Encodes a tree to a single string.(직렬화) | ||
| public String serialize(TreeNode root) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| dfsSerialize(root, sb); | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| private void dfsSerialize(TreeNode node, StringBuilder sb) { | ||
| if (node == null) { | ||
| sb.append("null").append(","); | ||
| return; | ||
| } | ||
| sb.append(node.val).append(","); | ||
|
|
||
| dfsSerialize(node.left, sb); | ||
| dfsSerialize(node.right, sb); | ||
| } | ||
|
|
||
| // Decodes your encoded data to tree.(역직렬화) | ||
| public TreeNode deserialize(String data) { | ||
| String[] arr = data.split(","); | ||
| index = 0; | ||
| return dfsDeserialize(arr); | ||
| } | ||
|
|
||
| private TreeNode dfsDeserialize(String[] arr) { | ||
| String value = arr[index++]; | ||
|
|
||
| if (value.equals("null")) { | ||
| return null; | ||
| } | ||
|
|
||
| TreeNode node = new TreeNode(Integer.parseInt(value)); | ||
|
|
||
| node.left = dfsDeserialize(arr); | ||
| node.right = dfsDeserialize(arr); | ||
| 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.eraseOverlapIntervals— Time: O(n log n) / Space: O(1)피드백: 배열 정렬이 O(n log n)이고, 이후 한 번 순회하는 O(n)이므로 전체 시간 복잡도는 O(n log n)입니다. 정렬 후 별도 자료구조를 사용하지 않으므로 공간 복잡도는 O(1)입니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.eraseOverlapIntervals— Time: O(n log n) / Space: O(1)피드백: 배열 정렬이 O(n log n)이고, 이후 한 번 순회하는 O(n)이므로 전체 시간 복잡도는 O(n log n)입니다. 정렬 후 별도 자료구조를 사용하지 않으므로 공간 복잡도는 O(1)입니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 3:
Solution.eraseOverlapIntervals— Time: ✅ O(n log n) → O(n log n) / Space: ❌ O(n) → O(1)피드백: 배열 정렬이 O(n log n)이고, 이후 한 번 순회하는 O(n)이므로 전체 시간 복잡도는 O(n log n)입니다. 정렬 후 별도 자료구조를 사용하지 않으므로 공간 복잡도는 O(1)입니다.
개선 제안: 현재 구현이 적절해 보입니다.