-
-
Notifications
You must be signed in to change notification settings - Fork 336
[juhui-jeong] WEEK 13 Solutions #2616
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
base: main
Are you sure you want to change the base?
Changes from all commits
54c671f
29c5f97
aafcc6c
acd63fc
7a0dd11
7498087
5fcce19
060d7d4
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,38 @@ | ||
| /** | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(n) | ||
| */ | ||
| class Solution { | ||
| public int[][] insert(int[][] intervals, int[] newInterval) { | ||
| List<int[]> result = new ArrayList<>(); | ||
|
|
||
| int newStart = newInterval[0]; | ||
| int newEnd = newInterval[1]; | ||
| boolean inserted = false; | ||
|
|
||
| for (int i = 0; i < intervals.length; i++) { | ||
| int curStart = intervals[i][0]; | ||
| int curEnd = intervals[i][1]; | ||
|
|
||
| if (curEnd < newStart) { | ||
| // 1. newInterval 보다 왼쪽 | ||
| result.add(new int[]{curStart, curEnd}); | ||
| } else if (curStart > newEnd) { | ||
| // 2. newInterval 보다 오른쪽 | ||
| if (!inserted) { | ||
| result.add(new int[]{newStart, newEnd}); | ||
| inserted = true; | ||
| } | ||
| result.add(new int[]{curStart, curEnd}); | ||
| } else { | ||
| newStart = Math.min(newStart, curStart); | ||
| newEnd = Math.max(newEnd, curEnd); | ||
| } | ||
| } | ||
|
|
||
| if (!inserted) { | ||
| result.add(new int[]{newStart, newEnd}); | ||
| } | ||
| return result.toArray(new int[result.size()][]); | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 중위 순회로 모든 노드를 방문하여 정렬된 리스트를 만들기 때문에 시간과 공간 복잡도는 각각 O(n)입니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(n) | ||
| */ | ||
| class Solution { | ||
| public int kthSmallest(TreeNode root, int k) { | ||
| List<Integer> list = new ArrayList<>(); | ||
| inorder(root, list); | ||
|
|
||
| return list.get(k-1); | ||
| } | ||
| private void inorder(TreeNode node, List<Integer>list) { | ||
| if (node == null) return; | ||
|
|
||
| inorder(node.left, list); | ||
| list.add(node.val); | ||
| inorder(node.right, list); | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: BST의 특성을 이용하여 루트에서부터 탐색하며, 최악의 경우 트리 높이 h만큼의 재귀 또는 반복이 필요하므로 시간 복잡도는 O(h), 공간은 재귀 호출 스택 또는 반복 변수에 따라 O(h) 또는 O(1)입니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 구간 정렬 후 한 번의 순회로 겹치는 구간을 계산하므로 시간 복잡도는 정렬에 따른 O(n log n), 공간은 상수입니다. 개선 제안: 현재 구현이 적절해 보입니다. |
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 빠른 포인터를 n만큼 이동시키고, 느린 포인터와 함께 한 번 순회하여 제거 위치를 찾기 때문에 시간 복잡도는 O(n), 공간 복잡도는 O(1)입니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 모든 노드를 방문하는 재귀 또는 반복으로 구조와 값을 비교하므로 시간 복잡도는 O(n), 공간은 재귀 호출 또는 큐의 크기에 따라 O(h)입니다. 개선 제안: 현재 구현이 적절해 보입니다. |
|
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)입니다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2: Codec.deserialize — Time: O(n) / Space: O(n)
| 복잡도 | |
|---|---|
| Time | O(n) |
| Space | 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 단일 순회로 구간들을 검사하며 새 구간과 겹치는 구간을 병합하거나 위치에 맞게 삽입하므로 시간 복잡도는 O(n), 추가 공간은 결과 리스트 저장을 위해 O(n)입니다.
개선 제안: 현재 구현이 적절해 보입니다.