-
-
Notifications
You must be signed in to change notification settings - Fork 245
[강희찬] WEEK 11 Solution #546
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4574e94
Feat: 104. Maximum Depth of Binary Tree
HC-kang 6dfa0c4
Merge branch 'DaleStudy:main' into main
HC-kang 43da9ea
Typo: 오탈자 수정
HC-kang 0b08e22
Merge branch 'main' of https://github.com/HC-kang/leetcode-study
HC-kang 7e77421
Feat: 143. Reorder List
HC-kang 5ba1835
Feat: 261. Graph Valid Tree
HC-kang 7c15d7e
Feat: 57. Insert Interval
HC-kang 86a8b13
Feat: 124. Binary Tree Maximum Path Sum
HC-kang 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,65 @@ | ||
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; | ||
} | ||
} | ||
|
||
/** | ||
* https://leetcode.com/problems/binary-tree-maximum-path-sum | ||
* T.C. O(n) | ||
* S.C. O(n) | ||
*/ | ||
function maxPathSum(root: TreeNode | null): number { | ||
let max = -Infinity; | ||
|
||
function dfs(node: TreeNode | null): number { | ||
if (!node) return 0; | ||
|
||
const left = Math.max(0, dfs(node.left)); | ||
const right = Math.max(0, dfs(node.right)); | ||
|
||
max = Math.max(max, node.val + left + right); | ||
|
||
return node.val + Math.max(left, right); | ||
} | ||
|
||
dfs(root); | ||
|
||
return max; | ||
} | ||
|
||
/** | ||
* iterative using stack | ||
* T.C. O(n) | ||
* S.C. O(n) | ||
*/ | ||
function maxPathSum(root: TreeNode | null): number { | ||
let max = -Infinity; | ||
const stack: Array<TreeNode | null> = [root]; | ||
const memo = new Map<TreeNode, number>(); | ||
|
||
while (stack.length) { | ||
const node = stack.pop(); | ||
|
||
if (!node) continue; | ||
|
||
if (memo.has(node)) { | ||
const left = Math.max(0, node.left ? memo.get(node.left)! : 0); | ||
const right = Math.max(0, node.right ? memo.get(node.right)! : 0); | ||
|
||
max = Math.max(max, node.val + left + right); | ||
|
||
memo.set(node, node.val + Math.max(left, right)); | ||
} else { | ||
stack.push(node, node.right, node.left); | ||
memo.set(node, 0); | ||
} | ||
} | ||
|
||
return max; | ||
} |
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,33 @@ | ||
/** | ||
* https://leetcode.com/problems/graph-valid-tree | ||
* T.C. O(n) | ||
* S.C. O(n) | ||
*/ | ||
function validTree(n: number, edges: number[][]): boolean { | ||
if (edges.length !== n - 1) return false; | ||
|
||
const parent = Array.from({ length: n }, (_, i) => i); | ||
|
||
// find and compress path | ||
function find(x: number): number { | ||
if (parent[x] !== x) { | ||
parent[x] = find(parent[x]); | ||
} | ||
return parent[x]; | ||
} | ||
|
||
// union two sets and check if they have the same root | ||
function union(x: number, y: number): boolean { | ||
const rootX = find(x); | ||
const rootY = find(y); | ||
if (rootX === rootY) return false; | ||
parent[rootX] = rootY; | ||
return true; | ||
} | ||
|
||
for (const [x, y] of edges) { | ||
if (!union(x, y)) 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* https://leetcode.com/problems/insert-interval | ||
* T.C. O(n) | ||
* S.C. O(n) | ||
*/ | ||
function insert(intervals: number[][], newInterval: number[]): number[][] { | ||
const result: number[][] = []; | ||
|
||
for (const [start, end] of intervals) { | ||
if (end < newInterval[0]) { | ||
result.push([start, end]); | ||
} else if (newInterval[1] < start) { | ||
result.push(newInterval); | ||
newInterval = [start, end]; | ||
} else { | ||
newInterval[0] = Math.min(newInterval[0], start); | ||
newInterval[1] = Math.max(newInterval[1], end); | ||
} | ||
} | ||
|
||
result.push(newInterval); | ||
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,55 @@ | ||
// 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; | ||
// } | ||
// } | ||
|
||
/** | ||
* https://leetcode.com/problems/maximum-depth-of-binary-tree | ||
* using recursion | ||
* T.C. O(n) | ||
* S.C. O(n) | ||
*/ | ||
function maxDepth(root: TreeNode | null): number { | ||
return dfs(root, 0); | ||
} | ||
|
||
function dfs(node: TreeNode | null, depth: number): number { | ||
if (!node) { | ||
return depth; | ||
} | ||
return Math.max(dfs(node.left, depth + 1), dfs(node.right, depth + 1)); | ||
} | ||
|
||
/** | ||
* using stack | ||
* T.C. O(n) | ||
* S.C. O(n) | ||
*/ | ||
function maxDepth(root: TreeNode | null): number { | ||
if (!root) { | ||
return 0; | ||
} | ||
|
||
let max = 0; | ||
|
||
const stack: [TreeNode | null, number][] = []; | ||
stack.push([root, 1]); | ||
|
||
while (stack.length) { | ||
const [node, depth] = stack.pop()!; | ||
|
||
if (node) { | ||
max = Math.max(max, depth); | ||
stack.push([node.left, depth + 1]); | ||
stack.push([node.right, depth + 1]); | ||
} | ||
} | ||
|
||
return max; | ||
} |
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 @@ | ||
class ListNode { | ||
val: number; | ||
next: ListNode | null; | ||
constructor(val?: number, next?: ListNode | null) { | ||
this.val = val === undefined ? 0 : val; | ||
this.next = next === undefined ? null : next; | ||
} | ||
} | ||
|
||
/** | ||
* https://leetcode.com/problems/reorder-list | ||
* T.C. O(n) | ||
* S.C. O(1) | ||
*/ | ||
function reorderList(head: ListNode | null): void { | ||
if (!head || !head.next) return; | ||
|
||
let fast: ListNode | null = head; | ||
let slow: ListNode | null = head; | ||
|
||
while (fast && fast.next) { | ||
fast = fast.next.next; | ||
slow = slow!.next; | ||
} | ||
|
||
let prev: ListNode | null = null; | ||
let curr: ListNode | null = slow; | ||
while (curr) { | ||
const next = curr.next; | ||
curr.next = prev; | ||
prev = curr; | ||
curr = next; | ||
} | ||
|
||
let front: ListNode | null = head; | ||
let back: ListNode | null = prev; | ||
while (back!.next) { | ||
const frontNext = front!.next; | ||
const backNext = back!.next; | ||
|
||
front!.next = back; | ||
back!.next = frontNext; | ||
|
||
front = frontNext; | ||
back = backNext; | ||
} | ||
} |
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.
모임 때 union-find 알고리즘에 대해서 소개를 부탁드려도 될까요?
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.
넵 알겠습니다~!