-
-
Notifications
You must be signed in to change notification settings - Fork 245
[YeomChaeeun] Week 13 #1071
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
[YeomChaeeun] Week 13 #1071
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,38 @@ | ||
/** | ||
* 중간에 새로운 구간 추가하기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) | ||
* - 공간 복잡도: O(n) | ||
* @param intervals | ||
* @param newInterval | ||
*/ | ||
function insert(intervals: number[][], newInterval: number[]): number[][] { | ||
if(intervals.length === 0) return [newInterval] | ||
let result: number[][] = [] | ||
|
||
let i = 0; | ||
|
||
// 1. newIntervals 이전 구간 추가 | ||
while(i < intervals.length && intervals[i][1] < newInterval[0]) { | ||
result.push(intervals[i]) | ||
i++ | ||
} | ||
|
||
// 2. 겹치는 부분 추가 | ||
const merged = [...newInterval] | ||
while(i < intervals.length && intervals[i][0] <= newInterval[1]) { | ||
merged[0] = Math.min(merged[0], intervals[i][0]) | ||
merged[1] = Math.max(merged[1], intervals[i][1]) | ||
i++ | ||
} | ||
result.push(merged) | ||
|
||
// 3. 이후의 구간 추가 | ||
while(i < intervals.length) { | ||
result.push(intervals[i]) | ||
i++ | ||
} | ||
|
||
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. 공간 복잡도 O(n) 에서 n이 가리키는건 어떤걸까요? :) 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. 노드의 개수입니다! |
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,47 @@ | ||
/** | ||
* 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) | ||
* } | ||
* } | ||
*/ | ||
/** | ||
* Binary Search Tree 특성을 고려하여 k번째 작은 숫자를 찾기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) | ||
* - 공간 복잡도: O(n) | ||
*/ | ||
function kthSmallest(root: TreeNode | null, k: number): number { | ||
let count = 0; | ||
let result = 0; | ||
|
||
/* | ||
BST의 특성 | ||
- 왼쪽 서브트리의 노드 값 < 현재 노드 값 | ||
- 현재 노드값 < 왼쪽 서브트리의 노드 값 | ||
=> 중위 순회시 오름차 순 방문 | ||
*/ | ||
|
||
// 중위 순회 함수 | ||
function inorder(node: TreeNode | null): void { | ||
if (node === null) return; | ||
inorder(node.left); | ||
|
||
count++; | ||
if (count === k) { | ||
result = node.val; | ||
return; | ||
} | ||
|
||
inorder(node.right); | ||
} | ||
|
||
inorder(root); | ||
return result; | ||
} |
34 changes: 34 additions & 0 deletions
34
lowest-common-ancestor-of-a-binary-search-tree/YeomChaeeun.ts
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 @@ | ||
/** | ||
* 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) | ||
* } | ||
* } | ||
*/ | ||
/** | ||
* BST 이진트리에 두 노드가 주어졌을 때, 근접한 부모 찾기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) | ||
* - 공간 복잡도: O(n) | ||
* @param root | ||
* @param p | ||
* @param q | ||
*/ | ||
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null { | ||
if(root === null || p === null || q === null) 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 { | ||
// 현재 노드가 양쪽 방향으로 있거나 현재 노드가 p, q인 경우 | ||
return root | ||
} | ||
} |
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.
시간, 공간 복잡도 모두 최적화 하여 잘 풀어내 주셨네요!