-
-
Notifications
You must be signed in to change notification settings - Fork 336
[hyeri0903] WEEK12 Solutions #2614
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 |
|---|---|---|
| @@ -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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 빠른 포인터를 n칸 이동시킨 후, 느린 포인터와 함께 끝까지 이동하며 제거 대상 노드 위치를 찾는다. 한 번의 순회로 해결 가능하다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| 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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 트리의 모든 노드를 한 번씩 방문하는 재귀 방식으로, 트리의 높이만큼 호출 스택이 쌓인다. 구조와 값 비교를 동시에 수행한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 배열 정렬 후 한 번 순회하며 겹침 여부를 체크하는 방식으로, 정렬이 시간 복잡도에 가장 큰 영향을 미친다. 정렬 후 순회는 O(n).
개선 제안: 현재 구현이 적절해 보입니다.