-
-
Notifications
You must be signed in to change notification settings - Fork 245
[mallayon] Week 13 #1069
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
[mallayon] Week 13 #1069
Changes from 4 commits
c0037ca
34a43b9
624081b
1a19296
fd523be
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,34 @@ | ||
/** | ||
*@link https://leetcode.com/problems/insert-interval/description/ | ||
* | ||
* 접근 방법 : | ||
* - 새로운 interval을 기존 interval에 추가하고, 시작 지점 기준으로 오름차순 정렬 | ||
* - 현재 interval이 result의 마지막 interval과 겹치는 경우, 종료 지점 업데이트해서 병함 | ||
* - 겹치지 않으면, result 배열에 현재 interval 추가 | ||
* | ||
* 시간복잡도 : O(nlogn) | ||
* - n = intervals 개수 | ||
* - 오름차순으로 정렬 | ||
* | ||
* 공간복잡도 : O(n) | ||
* - n = 병합 후 result 배열에 담긴 인터벌의 개수 | ||
*/ | ||
function insert(intervals: number[][], newInterval: number[]): number[][] { | ||
const sortedIntervals = [...intervals, newInterval].sort( | ||
(a, b) => a[0] - b[0] | ||
); | ||
const result: number[][] = [sortedIntervals[0]]; | ||
|
||
for (let i = 1; i < sortedIntervals.length; i++) { | ||
const lastInterval = result[result.length - 1]; | ||
const currentInterval = sortedIntervals[i]; | ||
|
||
if (currentInterval[0] <= lastInterval[1]) { | ||
lastInterval[1] = Math.max(currentInterval[1], lastInterval[1]); | ||
} else { | ||
result.push(currentInterval); | ||
} | ||
} | ||
|
||
return result; | ||
} |
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. 변수 count 를 사용하여 공간 복잡도를 O(h) h 는 재귀스택, 으로 잘 풀어내신것 같습니다! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* class TreeNode { | ||
* val: number | ||
* left: TreeNode | null | ||
* right: TreeNode | null | ||
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
* } | ||
*/ | ||
|
||
/** | ||
*@link https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ | ||
* | ||
* 접근 방법 : | ||
* - 작은 수대로 정렬해야 하니까 중위순회로 트리 순회 | ||
* - 왼쪽 모드 끝까지 방문 -> 중간 노드 -> 오른쪽 끝까지 방문 으로 진행 | ||
* - k번째 노드 체크하기 위해서 노드 방문할 때마다 count 업데이트 | ||
* | ||
* 시간복잡도 : O(n) | ||
* - n = 노드의 개수, | ||
* - 최악의 경우 전체 트리 탐색 | ||
* | ||
* 공간복잡도 : O(n) | ||
* - 재귀 호출이 트리 깊이만큼 스택 쌓임. | ||
* - 기울어진 트리의 경우 O(n) | ||
*/ | ||
function kthSmallest(root: TreeNode | null, k: number): number { | ||
let count = 0; | ||
let result: number | null = null; | ||
|
||
const inOrderTraverse = (node: TreeNode | null) => { | ||
if (!node) return null; | ||
|
||
inOrderTraverse(node.left); | ||
|
||
count++; | ||
if (count === k) { | ||
result = node.val; | ||
return; | ||
} | ||
|
||
inOrderTraverse(node.right); | ||
}; | ||
|
||
inOrderTraverse(root); | ||
|
||
return result!; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
*@link https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ | ||
* | ||
* 접근 방법 : | ||
* - BST니까 p,q 노드를 root 노드와 비교해서 탐색 범위 좁히기 | ||
* - root.val보다 작은 경우 왼쪽 하위 트리 탐색 | ||
* - root.vale보다 큰 경우 오른쪽 하위 트리 탐색 | ||
* - 그 외의 경우는 값이 같으니까 root 노드 리턴 | ||
* | ||
* 시간복잡도 : O(n) | ||
* - 균형 잡힌 BST의 경우 O(logn) | ||
* - 한쪽으로 치우친 트리의 경우 O(n) | ||
* | ||
* 공간복잡도 : O(n) | ||
* - 재귀 호출 스택 크기가 트리 깊이에 비례 | ||
* - 균형 잡힌 BST의 경우 O(logn) | ||
* - 한쪽으로 치우친 트리의 경우 O(n) | ||
*/ | ||
class TreeNode { | ||
val: number; | ||
left: TreeNode | null; | ||
right: TreeNode | null; | ||
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
this.val = val === undefined ? 0 : val; | ||
this.left = left === undefined ? null : left; | ||
this.right = right === undefined ? null : right; | ||
} | ||
} | ||
|
||
function lowestCommonAncestor( | ||
root: TreeNode | null, | ||
p: TreeNode | null, | ||
q: TreeNode | null | ||
): TreeNode | null { | ||
if (!root || !p || !q) return null; | ||
|
||
if (p.val < root.val && q.val < root.val) | ||
return lowestCommonAncestor(root.left, p, q); | ||
else if (p.val > root.val && q.val > root.val) | ||
return lowestCommonAncestor(root.right, p, q); | ||
else return root; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
*@link https://leetcode.com/problems/meeting-rooms/description/ | ||
* | ||
* 접근 방법 : | ||
* - 미팅 시작 시간이 빠른 순으로 정렬 | ||
* - 현재 미팅 시작 시간이 이전 미팅 끝나는 시간보다 작으면 겹치는 것이므로 false 리턴 | ||
* | ||
* 시간복잡도 : O(nlogn) | ||
* - n = intervals의 길이, 정렬했으므로 O(nlogn) | ||
* | ||
* 공간복잡도 : O(1) | ||
* - 고정된 변수만 사용 | ||
*/ | ||
|
||
function canAttendMeetings(intervals: number[][]): boolean { | ||
intervals.sort((a, b) => a[0] - b[0]); | ||
|
||
for (let i = 1; i < intervals.length; i++) { | ||
const previousMeetingTime = intervals[i - 1]; | ||
const currentMeetingTime = intervals[i]; | ||
if (currentMeetingTime[0] < previousMeetingTime[1]) return false; | ||
} | ||
|
||
return true; | ||
} | ||
Comment on lines
+15
to
+25
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. @mmyeon 님 안녕하세요. 저도 같은 방식으로 문제를 풀었습니다. 아직 문제를 푸는 중이신 것 같네요! 주말까지 파이팅입니다 👍 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. @KwonNayeon 님 안녕하세요! |
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) 으로 풀이 하려면 어떻게 하면 될까요 :)
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.
@TonyKim9401
재정렬하던 로직 제거하고, newInterval를 intervals 배열안에 삽입하도록 개선했습니다!
꼼꼼한 리뷰 감사합니다 :)