-
-
Notifications
You must be signed in to change notification settings - Fork 247
[moonhyeok] Week 13 #1075
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
[moonhyeok] Week 13 #1075
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bd49386
feat: Upload lowest-common-ancestor-of-a-binary-search-tree (typescript)
mike2ox 47ecd03
Added README.md file for Lowest Common Ancestor of a Binary Search Tree
mike2ox b45c568
Time: 78 ms (43.70%) | Memory: 68 MB (6.53%) - LeetSync
mike2ox 8397bef
feat: Delete invalid files
mike2ox 7e7086f
feat: Upload insert-interval (typescript)
mike2ox 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,29 @@ | ||
/** | ||
* Source: https://leetcode.com/problems/insert-interval/ | ||
* 풀이방법: 미리 정렬 후 끝 부분만 비교하면서 갱신시키기 | ||
* 시간복잡도: O(nlogn) - 정렬때문에 | ||
* 공간복잡도: O(n) | ||
* | ||
* 통과시간 | ||
* - 최초: 40분 | ||
*/ | ||
function insert(intervals: number[][], newInterval: number[]): number[][] { | ||
const mergedIntervals = [...intervals, newInterval]; | ||
const result: number[][] = []; | ||
mergedIntervals.sort((a, b) => a[0] - b[0]); | ||
|
||
for (const interval of mergedIntervals) { | ||
// 결과 배열이 비어있거나 현재 구간이 마지막 구간과 겹치지 않는 경우 | ||
if (result.length === 0 || interval[0] > result[result.length - 1][1]) { | ||
result.push(interval); | ||
} else { | ||
// 결과물의 마지막 구간의 큰값 범위와 현재 구간의 큰값 범위를 비교후 큰 값으로 대체하기 | ||
result[result.length - 1][1] = Math.max( | ||
result[result.length - 1][1], | ||
interval[1] | ||
); | ||
} | ||
} | ||
|
||
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,69 @@ | ||
/** | ||
* Source: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree | ||
* Solution: DFS를 이용해서 두 노드까지의 경로를 구해 순차적으로 비교하면서 가장 마지막 동일 노드를 추출 | ||
* | ||
* 시간복잡도: O(N) - 최악인 경우, 전체 노드수 탐색 | ||
* 공간복잡도: O(N) - 최악인 경우, 전체 노드수 보관 | ||
* | ||
* 다른 풀이 | ||
* - 재귀로도 해결할 것으로 보이지만 바로 구현체가 떠오르지 않음 | ||
*/ | ||
|
||
/** | ||
* 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) | ||
* } | ||
* } | ||
*/ | ||
|
||
function lowestCommonAncestor( | ||
root: TreeNode | null, | ||
p: TreeNode | null, | ||
q: TreeNode | null | ||
): TreeNode | null { | ||
if (!root || !p || !q) return null; | ||
let stack = new Array(root); | ||
let pRoute: Array<TreeNode> | null; | ||
let qRoute: Array<TreeNode> | null; | ||
let answer: TreeNode | null; | ||
const visited = new Set(); | ||
|
||
while (stack.length) { | ||
const left = stack.at(-1).left; | ||
if (left && !visited.has(left.val)) { | ||
stack.push(left); | ||
continue; | ||
} | ||
const right = stack.at(-1).right; | ||
if (right && !visited.has(right.val)) { | ||
stack.push(right); | ||
continue; | ||
} | ||
const now = stack.pop(); | ||
visited.add(now.val); | ||
if (now.val === q.val) { | ||
qRoute = [...stack, now]; | ||
continue; | ||
} | ||
if (now.val === p.val) { | ||
pRoute = [...stack, now]; | ||
continue; | ||
} | ||
} | ||
const shortLength = | ||
pRoute.length > qRoute.length ? qRoute.length : pRoute.length; | ||
for (let i = 0; i < shortLength; i++) { | ||
if (pRoute.at(i) !== qRoute.at(i)) { | ||
answer = pRoute.at(i - 1); | ||
break; | ||
} | ||
} | ||
return answer ? answer : pRoute.at(shortLength - 1); | ||
} |
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.
최초 통과 시간: 42분