-
-
Notifications
You must be signed in to change notification settings - Fork 336
[hyeri0903] WEEK 13 Solutions #2621
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
e44e93b
2b06511
33aeb1d
e180582
b56c362
44beec8
ee8428c
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,41 @@ | ||
| class Solution { | ||
| public int[][] insert(int[][] intervals, int[] newInterval) { | ||
| /** | ||
| 1.문제: newInterval 을 추가 후 intervals 오름차순 재정렬 | ||
| 2.constraints: start 기준으로 ascending order | ||
| 3.solutions | ||
| - newIntervals start 보다 앞에있는 것들은 merge 필요 없음 | ||
| - 즉 intervalse[i][1] < newInterval[0] 이면 merge X | ||
| - intervals[i][0] <= newInterval[1] 이면 merge | ||
| - 마지막에 겹치는 부분은 skip 후 intervals 재정렬 | ||
| time: O(n), space: O(1) | ||
| */ | ||
|
|
||
| List<int []> result = new ArrayList<>(); | ||
| int i = 0; | ||
| int n = intervals.length; | ||
|
|
||
| //1.newInterval 앞에 있는 값은 그대로 result add | ||
| while(i < n && intervals[i][1] < newInterval[0]) { | ||
| result.add(intervals[i]); | ||
| i += 1; | ||
| } | ||
|
|
||
| //2.overlap 구간은 merge | ||
| while(i < n && intervals[i][0] <= newInterval[1]) { | ||
| newInterval[0] = Math.min(newInterval[0], intervals[i][0]); | ||
| newInterval[1] = Math.max(newInterval[1], intervals[i][1]); | ||
| i += 1; | ||
| } | ||
| result.add(newInterval); | ||
|
|
||
| //3.나머지 result에 add | ||
| while(i < n) { | ||
| result.add(intervals[i]); | ||
| i++; | ||
| } | ||
|
|
||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 이진 탐색 트리의 중위 순회는 정렬된 순서로 노드를 방문하므로, k번째 노드를 찾기 위해 모든 노드를 탐색할 수 있으며, 재귀 호출의 깊이는 트리의 높이입니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /** | ||
| * Definition for a binary tree node. | ||
| * public class TreeNode { | ||
| * int val; | ||
| * TreeNode left; | ||
| * TreeNode right; | ||
| * TreeNode() {} | ||
| * TreeNode(int val) { this.val = val; } | ||
| * TreeNode(int val, TreeNode left, TreeNode right) { | ||
| * this.val = val; | ||
| * this.left = left; | ||
| * this.right = right; | ||
| * } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| private int count = 0; | ||
| private int answer = 0; | ||
| public int kthSmallest(TreeNode root, int k) { | ||
| /** | ||
| 1.BST에서 K번째 가장 작은 값 찾기 | ||
| 2.constraints : node 개수 ㅡin = 0, max = 10000 | ||
| 3.solutions: | ||
| - k번째 방문한 노드가 k번째로 작은 값 (bst 이므로) | ||
| - dfs search (inorder traversal) | ||
| */ | ||
|
|
||
| dfs(root, k); | ||
| return answer; | ||
| } | ||
| private void dfs(TreeNode node, int k) { | ||
| if(node == null) { | ||
| return; | ||
| } | ||
|
|
||
|
|
||
| dfs(node.left, k); | ||
| count++; | ||
|
|
||
| if(count == k) { | ||
| answer = node.val; | ||
| return; | ||
| } | ||
|
|
||
| dfs(node.right, k); | ||
| } | ||
|
|
||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 트리의 모든 노드를 방문하며 구조와 값을 비교하는 재귀 방식으로, 최악의 경우 모든 노드를 방문하므로 시간은 선형입니다. 공간은 재귀 호출 스택에 따라 트리의 높이만큼 사용됩니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 리스트를 한 번 순회하며 조건에 따라 구간을 분리하고 병합하는 방식으로, 최악의 경우 모든 구간을 저장하므로 시간과 공간 모두 선형입니다.
개선 제안: 현재 구현이 적절해 보입니다.