-
-
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
| @@ -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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: intervals 배열을 정렬하는데 O(n log n) 시간이 소요되고, 이후 한 번 순회하며 겹침 여부를 체크하므로 전체 시간 복잡도는 O(n log n)이다. 공간은 정렬에 필요한 상수 공간만 사용한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| class Solution { | ||
| public int eraseOverlapIntervals(int[][] intervals) { | ||
| /** | ||
| 1.prob: 겹치지 않는 최소한의 non-oerlapping interval 제거 | ||
| 2.constraints | ||
| - inverval.lenght min=1, max = 100,000 | ||
| 3.solutions - Greedy | ||
| - end 기준 오름차순 정렬 | ||
| - 이전 interval end 저장 | ||
| - 다음 interval check, 안겹치면 -> 선택, 겹치면 count++ | ||
| Time: O(n logn), Space: O(1) | ||
| */ | ||
| int count = 0; | ||
|
|
||
| //end 기준 ascending 정렬 | ||
| Arrays.sort(intervals, (a, b) -> a[1] - b[1]); | ||
| int end = intervals[0][1]; | ||
|
|
||
| //겹치면 count++, 안겹치면 end update | ||
| for(int i = 1; i < intervals.length; i++) { | ||
| if(intervals[i][0] < end) { | ||
| count++; | ||
| } else { | ||
| end = intervals[i][1]; | ||
| } | ||
| } | ||
|
|
||
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 이중 포인터를 사용하여 한 번의 순회로 해결하며, 리스트 길이와 상관없이 일정한 공간을 사용한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * public class ListNode { | ||
| * int val; | ||
| * ListNode next; | ||
| * ListNode() {} | ||
| * ListNode(int val) { this.val = val; } | ||
| * ListNode(int val, ListNode next) { this.val = val; this.next = next; } | ||
| * } | ||
| */ | ||
| class Solution { | ||
| public ListNode removeNthFromEnd(ListNode head, int n) { | ||
| /** | ||
| 1.linked list 에서 뒤에서 n번째 node remove | ||
| 2.constraints: | ||
| node 개수(sz) min=1, max=30 | ||
| n값 min = 1, max= sz | ||
| 3.solutions: two pointers | ||
| - fast, slow pointer 2개의 간격 = n | ||
| - fast 가 끝지점에 도달하면 그때 slow.next 를 제거 | ||
| time: O(n), space: O(1) | ||
| */ | ||
|
|
||
| ListNode dummy = new ListNode(0); | ||
| dummy.next = head; | ||
|
|
||
| ListNode fast = dummy; | ||
| ListNode slow = dummy; | ||
|
|
||
| //fast pointer n+1칸 이동, fast - slow = n | ||
| for(int i = 0; i <= n; i++) { | ||
| fast = fast.next; | ||
| } | ||
|
|
||
| while(fast != null) { | ||
| fast = fast.next; | ||
| slow = slow.next; | ||
| } | ||
|
|
||
| //slow pointer 의 다음 노드(slow.next) 제거 | ||
| slow.next = slow.next.next; | ||
|
|
||
| return dummy.next; | ||
|
|
||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 두 트리의 모든 노드를 방문하며, 최악의 경우 트리의 높이 h만큼의 재귀 호출 스택을 사용한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /** | ||
| * 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 { | ||
| public boolean isSameTree(TreeNode p, TreeNode q) { | ||
| /** | ||
| 1.problem: 2개의 bst 가 동일한지 체크 | ||
| 2.constraints - 구조와 노드가 동일해야된다 | ||
| 3.solution - DFS | ||
| 1) 두 트리의 노드가 모두 null -> true | ||
| 2) 둘중 하나만 null -> false | ||
| 3) 값이 다르면 false | ||
| 4)재귀로 트리의 구조가 동일한지 체크 | ||
| */ | ||
|
|
||
| if(p == null && q == null) { | ||
| return true; | ||
| } | ||
| if(p == null || q == null) { | ||
| return false; | ||
| } | ||
| if(p.val != q.val) { | ||
| return false; | ||
| } | ||
|
|
||
| if(isSameTree(p.left, q.left) && isSameTree(p.right, q.right)) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| } |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 이 구현은 재귀적 중위순회로, 최악의 경우 트리의 높이 h만큼의 스택 공간을 사용하며, 모든 노드를 방문하므로 시간 복잡도는 O(n)이다.
개선 제안: 현재 구현이 적절해 보입니다.