-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Merged
Merged
[mallayon] Week 13 #1069
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c0037ca
add solution : 252. Meeting Rooms
mmyeon 34a43b9
add solution : 235. Lowest Common Ancestor of a Binary Search Tree
mmyeon 624081b
add solution : 230. Kth Smallest Element in a BST
mmyeon 1a19296
add solution : 57. Insert Interval
mmyeon fd523be
apply feedback : improve O(nlogn) -> O(n)
mmyeon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@mmyeon 님 안녕하세요. 저도 같은 방식으로 문제를 풀었습니다. 아직 문제를 푸는 중이신 것 같네요! 주말까지 파이팅입니다 👍
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.
@KwonNayeon 님 안녕하세요!
이번 주 리뷰 요청이 늦었네요ㅠㅠ 시간 내어서 리뷰 해주셔서 정말 감사합니다!
이번 주도 문제 푸느라 정말 고생 많으셨어요 👍
벌써 13주라니 놀라워요. 남은 기간도 같이 힘내보아요. 즐거운 주말 되세요!! 😄