-
-
Notifications
You must be signed in to change notification settings - Fork 361
[dahyeong-yun] WEEK 12 Solutions #2859
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
|
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. end 기준으로 정렬한 후에 남길 구간의 개수를 세고, 전체 구간의 개수에서 빼주는 로직으로 이해했습니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * TC : O(n log n) | ||
| * - intervals 배열의 길이 n 을 최초 정렬 하므로 O(n log n) | ||
| * - 이후 for loop 는 O(n) | ||
| * SC : O(1) | ||
| * - 별도 유의미한 공간 할당은 없음 | ||
| */ | ||
| class Solution { | ||
| public int eraseOverlapIntervals(int[][] intervals) { | ||
| Arrays.sort(intervals, (a, b) -> Integer.compare(a[1], b[1])); | ||
|
|
||
| int count = 1; | ||
| int beforeEnd = intervals[0][1]; | ||
|
|
||
| for (int i = 1; i < intervals.length; i++) { | ||
| if (intervals[i][0] >= beforeEnd) { | ||
| count++; | ||
| beforeEnd = intervals[i][1]; | ||
| } | ||
| } | ||
| return intervals.length - 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. 🏷️ 알고리즘 패턴 분석remove-nth-node-from-end-of-list/dahyeong-yun.java/**
* TC: O(n)
* - ListNode의 길이 n 만큰 순회하므로 O(n)
* SC: O(n)
* - ListNode의 길이 n 만큼 ArrayList 할당하므로 O(n)
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
List<ListNode> list = new ArrayList<>();
ListNode cursor = head.next;
list.add(head);
while (cursor != null) {
list.add(cursor);
cursor = cursor.next;
}
int sz = list.size();
int deleteTarget = list.size() - n;
if (deleteTarget < 0)
return null;
if (deleteTarget - 1 >= 0 && deleteTarget <= sz - 2) {
list.get(deleteTarget - 1).next = list.get(deleteTarget + 1);
} else if(deleteTarget == 0) {
head = head.next;
} else {
list.get(deleteTarget - 1).next = null;
}
return head;
}
}
📊 시간/공간 복잡도 분석
피드백: 리스트를 배열에 저장하므로 추가 공간이 필요하지만 구현은 직관적입니다. 개선 제안: 공간 복잡도를 줄이려면 더블 포인터(스피닝) 기법으로 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(1)로 개선할 수 있는 것 같습니다 ㅎㅎ
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /** | ||
| * TC: O(n) | ||
| * - ListNode의 길이 n 만큰 순회하므로 O(n) | ||
| * SC: O(n) | ||
| * - ListNode의 길이 n 만큼 ArrayList 할당하므로 O(n) | ||
| */ | ||
| class Solution { | ||
| public ListNode removeNthFromEnd(ListNode head, int n) { | ||
| List<ListNode> list = new ArrayList<>(); | ||
| ListNode cursor = head.next; | ||
| list.add(head); | ||
|
|
||
| while (cursor != null) { | ||
| list.add(cursor); | ||
| cursor = cursor.next; | ||
| } | ||
|
|
||
| int sz = list.size(); | ||
| int deleteTarget = list.size() - n; | ||
| if (deleteTarget < 0) | ||
| return null; | ||
|
|
||
| if (deleteTarget - 1 >= 0 && deleteTarget <= sz - 2) { | ||
| list.get(deleteTarget - 1).next = list.get(deleteTarget + 1); | ||
| } else if(deleteTarget == 0) { | ||
| head = head.next; | ||
| } else { | ||
| list.get(deleteTarget - 1).next = null; | ||
| } | ||
| return head; | ||
| } | ||
| } |
|
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. 🏷️ 알고리즘 패턴 분석same-tree/dahyeong-yun.java/**
* TC : O(n)
* - TreeNode의 노드 수 n 만큼 순회하므로 O(n)
* SC : O(n)
* - TreeNode의 노드 높이 h 만큼 콜스택이 쌓이고, 편향 트리의 경우 O(n) 까지 공간이 필요
*/
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
// p와 q 둘다 null 인가?
if(p == null && q == null) {
return true;
} else if(p == null || q == null) { // 하나만 null 인가
return false;
} else if(p.val != q.val) {
return false;
} else {
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
}
}
📊 시간/공간 복잡도 분석
피드백: 최악의 경우 편향 트리에서 스택 깊이가 증가하므로 공간 복잡도는 트리의 높이와 같습니다. 개선 제안: 추가적인 최적화는 필요 없으며, 트리의 구조에 따른 일반적인 재귀 풀이에 해당합니다.
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. 재귀 사용해서 DFS로 깔끔하게 풀어주셨네요. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * TC : O(n) | ||
| * - TreeNode의 노드 수 n 만큼 순회하므로 O(n) | ||
| * SC : O(n) | ||
| * - TreeNode의 노드 높이 h 만큼 콜스택이 쌓이고, 편향 트리의 경우 O(n) 까지 공간이 필요 | ||
| */ | ||
| class Solution { | ||
| public boolean isSameTree(TreeNode p, TreeNode q) { | ||
| // p와 q 둘다 null 인가? | ||
| if(p == null && q == null) { | ||
| return true; | ||
| } else if(p == null || q == null) { // 하나만 null 인가 | ||
| return false; | ||
| } else if(p.val != q.val) { | ||
| return false; | ||
| } else { | ||
| return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); | ||
| } | ||
| } | ||
| } |
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.
🏷️ 알고리즘 패턴 분석
non-overlapping-intervals/dahyeong-yun.java
📊 시간/공간 복잡도 분석
피드백: 종료점을 기준으로 정렬하고, 이전 종료점 이후의 구간만 남겨두어 중복을 제거합니다.
개선 제안: 현재 구현이 적절해 보입니다.