-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #548 from obzva/main
[Flynn] week 11
- Loading branch information
Showing
5 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,61 @@ | ||
/* | ||
ํ์ด | ||
- post order traversal dfs๋ฅผ ํ์ฉํ์ฌ ํ์ดํ ์ ์์ต๋๋ค | ||
Big O | ||
- N: node์ ๊ฐ์ | ||
- H: Tree์ ๋์ด | ||
- Time compleixty: O(N) | ||
- ๋ชจ๋ node๋ฅผ ์ต๋ ํ ๋ฒ ํ์ํฉ๋๋ค | ||
- Space complexity: O(H) | ||
- ์ฌ๊ท ํธ์ถ ์คํ์ ๊น์ด๋ H์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
func maxPathSum(root *TreeNode) int { | ||
res := root.Val | ||
|
||
var maxSubtreeSum func(*TreeNode) int | ||
maxSubtreeSum = func(node *TreeNode) int { | ||
// base case | ||
if node == nil { | ||
return 0 | ||
} | ||
// left subtree๋ก ๋ด๋ ค๊ฐ์ ๋ ๊ตฌํ ์ ์๋ maximum path sum | ||
// left subtree๋ก path๋ฅผ ๋ด๋ ค๊ฐ์ ๋, left subtree๊ฐ sum์ ๊ธฐ์ฌํ๋ ๊ฐ์ด 0๋ณด๋ค ์์ ๊ฒฝ์ฐ | ||
// left subtree๋ก๋ path๋ฅผ ๋ด๋ ค๊ฐ์ง ์๋ ๊ฒ์ด ์ข์ | ||
// ๋ฐ๋ผ์ left < 0 ์ธ ๊ฒฝ์ฐ์ left = 0 | ||
left := maxSubtreeSum(node.Left) | ||
if left < 0 { | ||
left = 0 | ||
} | ||
// right subtree๋ left subtree์ ๋์ผํจ | ||
right := maxSubtreeSum(node.Right) | ||
if right < 0 { | ||
right = 0 | ||
} | ||
// ํ์ฌ ํ์ํ๊ณ ์๋ node์ ์กฐ์ node๋ฅผ path์ ํฌํจํ์ง ์๊ณ ๋ | ||
// maxPathSum์ด ๊ตฌํด์ง๋ ๊ฒฝ์ฐ๊ฐ ์์ | ||
if res < left+right+node.Val { | ||
res = left + right + node.Val | ||
} | ||
// ํ์ฌ๊น์ง ๊ณ์ฐํ subtree path sum์ ๋ถ๋ชจ node์๊ฒ ์ ๋ฌํด์ผ ํจ | ||
// ํ์ฌ node์ ๋ถ๋ชจ์ ์ด์ด์ง๋ path์ฌ์ผ ํ๋ฏ๋ก, node.Val + max(left, right)๋ฅผ ๋ฐํํ๋ฉด ๋จ | ||
subTreeSum := node.Val | ||
if left > right { | ||
subTreeSum += left | ||
} else { | ||
subTreeSum += right | ||
} | ||
return subTreeSum | ||
} | ||
|
||
maxSubtreeSum(root) | ||
return res | ||
} |
This file contains 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,57 @@ | ||
/* | ||
ํ์ด | ||
- valid tree์ธ์ง ํ๋ณํ๋ ๋ฌธ์ ์ ๋๋ค | ||
์ฃผ์ด์ง input์ด valid tree์ด๋ ค๋ฉด, | ||
1. cycle์ด ์์ด์ผ ํฉ๋๋ค (cycle์ด ์๋ ๊ฒฝ์ฐ: [[0, 1], [1, 2], [2, 0]]) | ||
2. ๋ชจ๋ node๊ฐ ์ฐ๊ฒฐ๋์ด ์์ด์ผ ํฉ๋๋ค (๋ชจ๋ node๊ฐ ์ฐ๊ฒฐ๋์ง ์์ ๊ฒฝ์ฐ: [[0, 1], [2, 3]]) | ||
- dfs ๋ฐฉ์์ ํจ์๋ฅผ ์ฌ๊ท ํธ์ถํ์ฌ ํ์ดํ ์ ์์ต๋๋ค | ||
Big O | ||
- N: n | ||
- E: ์ฃผ์ด์ง ๋ฐฐ์ด edges์ ํฌ๊ธฐ | ||
- Time complexity: O(N) | ||
- ๋ชจ๋ node๋ฅผ ์ต๋ 1๋ฒ์ฉ ํ์ํฉ๋๋ค | ||
- Space complexity: O(E + N) | ||
- visited์ ํฌ๊ธฐ๋ N์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค -> O(N) | ||
- adj์ ํฌ๊ธฐ๋ E์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค -> O(E) | ||
- dfs์ ์ฌ๊ทํธ์ถ ์คํ ๊น์ด๋ ์ต์ ์ ๊ฒฝ์ฐ N๊น์ง ์ปค์ง ์ ์์ต๋๋ค -> O(N) | ||
*/ | ||
|
||
func validTree(n int, edges [][]int) bool { | ||
// valid tree๋ n-1๊ฐ์ edge๋ฅผ ๊ฐ์ง ์ ๋ฐ์ ์์ตvalidTree | ||
// ์๋ ํ๋ณ์์ ์ด์ฉํ๋ฉด ์ ํจํ์ง ์์ input์ ๋ํด ์๋นํ ์ฐ์ฐ์ ์ค์ผ ์ ์์ต๋๋ค | ||
if len(edges) != n-1 { | ||
return false | ||
} | ||
// ์ฃผ์ด์ง 2์ฐจ์ ๋ฐฐ์ด edges๋ฅผ ์ด์ฉํด adjacency list๋ฅผ ์์ฑํฉ๋๋ค | ||
adj := make([][]int, n) | ||
for _, edge := range edges { | ||
adj[edge[0]] = append(adj[edge[0]], edge[1]) | ||
adj[edge[1]] = append(adj[edge[1]], edge[0]) | ||
} | ||
// cycle์ด ์๋์ง ์ฌ๋ถ๋ฅผ ํ๋จํ๊ธฐ ์ํด visited๋ผ๋ map์ ์์ฑํฉ๋๋ค (Go์์๋ map์ผ๋ก set ๊ธฐ๋ฅ์ ๋์ ํจ) | ||
visited := make(map[int]bool) | ||
|
||
var dfs func(int, int) bool | ||
dfs = func(node int, parent int) bool { | ||
// cycle ๋ฐ๊ฒฌ์ false return | ||
if _, ok := visited[node]; ok { | ||
return false | ||
} | ||
visited[node] = true | ||
for _, next := range adj[node] { | ||
if next == parent { | ||
continue | ||
} | ||
if !dfs(next, node) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
// cycle ์ฌ๋ถ๋ฅผ ํ๋จํฉ๋๋ค | ||
if !dfs(0, -1) { | ||
return false | ||
} | ||
// node๊ฐ ๋ชจ๋ ์ฐ๊ฒฐ๋์ด ์๋์ง ์ฌ๋ถ๋ฅผ ํ๋จํฉ๋๋ค | ||
return len(visited) == n | ||
} |
This file contains 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,89 @@ | ||
/* | ||
ํ์ด | ||
- newInterval์ start์ end์ ๋ํด ๊ฐ๊ฐ ์ด์งํ์์ ์งํํ์ฌ insertํ index๋ฅผ ์ฐพ์๋ผ ์ ์์ต๋๋ค | ||
- index์ ์๋ค interval๊ณผ newInterval์ ๋น๊ตํ์ฌ merge ์ฌ๋ถ๋ฅผ ํ๋ณํ๋ฉด ๋ฌธ์ ์์ ์ํ๋ ๋ฐฐ์ด์ ๋ง๋ค ์ ์์ต๋๋ค | ||
Big O | ||
- N: ์ฃผ์ด์ง ๋ฐฐ์ด intervals์ ๊ธธ์ด | ||
- Time complexity: O(N) | ||
- ์ด์ง ํ์ -> O(logN) | ||
- append(res, ...) -> O(N) | ||
- O(N + logN) = O(N) | ||
- Space complexity: O(N) | ||
- ๋ฐํํ๋ ๋ฐฐ์ด res์ ๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ๊ณ ๋ คํ๋ฉด O(N) | ||
*/ | ||
|
||
func insert(intervals [][]int, newInterval []int) [][]int { | ||
n := len(intervals) | ||
// base case | ||
if n == 0 { | ||
return append(intervals, newInterval) | ||
} | ||
// ์ด์งํ์ ํจ์ | ||
// isStart: newInterval์ start๋ฅผ ํ์ํ ๋ true, end๋ฅผ ํ์ํ ๋ false | ||
// target๋ณด๋ค ํฐ ๊ฐ ์ค์์ ๊ฐ์ฅ ์์ index๋ฅผ ๋ฐํํจ | ||
var binarySearch func(int, bool) int | ||
binarySearch = func(target int, isStart bool) int { | ||
lo := 0 | ||
hi := len(intervals) | ||
for lo < hi { | ||
mid := lo + (hi-lo)/2 | ||
if isStart { | ||
if intervals[mid][0] < target { | ||
lo = mid + 1 | ||
} else { | ||
hi = mid | ||
} | ||
} else { | ||
if intervals[mid][1] < target { | ||
lo = mid + 1 | ||
} else { | ||
hi = mid | ||
} | ||
} | ||
} | ||
return lo | ||
} | ||
|
||
start := binarySearch(newInterval[0], true) | ||
// newInterval์ ์์ ์ง์ ์ด intervals[start-1]์ ๋ ์ง์ ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ผ๋ฉด mergeํด์ผ ํจ | ||
mergeStart := start > 0 && newInterval[0] <= intervals[start-1][1] | ||
end := binarySearch(newInterval[1], false) - 1 | ||
// newInterval์ ๋ ์ง์ ์ด intervals[end+1]์ ์์ ์ง์ ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ผ๋ฉด mergeํด์ผ ํจ | ||
mergeEnd := end+1 < n && newInterval[1] >= intervals[end+1][0] | ||
|
||
// -Go์์์ ์ต์ ํ๋ฅผ ์ํ ์ฝ๋์ ๋๋ค- | ||
resCapacity := n + 1 | ||
if mergeStart { | ||
resCapacity-- | ||
} | ||
if mergeEnd { | ||
resCapacity-- | ||
} | ||
// ----------------------------- | ||
res := make([][]int, 0, resCapacity) | ||
// newInterval์ด ๋ค์ด๊ฐ index๋ณด๋ค ์ ๋ถ๋ถ์ ๊ฐ๋ค์ res์ append | ||
if mergeStart { | ||
res = append(res, intervals[:start-1]...) | ||
} else { | ||
res = append(res, intervals[:start]...) | ||
} | ||
// newInterval์ res์ append | ||
// mergeStart, mergeEnd ์ฌ๋ถ์ ๋ฐ๋ผ ๋ณํฉํ ์ง ๊ทธ๋๋ก ๋ฃ์์ง ํ๋จ | ||
if mergeStart && mergeEnd { | ||
res = append(res, []int{intervals[start-1][0], intervals[end+1][1]}) | ||
} else if mergeStart { | ||
res = append(res, []int{intervals[start-1][0], newInterval[1]}) | ||
} else if mergeEnd { | ||
res = append(res, []int{newInterval[0], intervals[end+1][1]}) | ||
} else { | ||
res = append(res, newInterval) | ||
} | ||
// newInterval์ด ๋ค์ด๊ฐ index๋ณด๋ค ๋ท ๋ถ๋ถ์ ๊ฐ๋ค์ res์ append | ||
if mergeEnd { | ||
res = append(res, intervals[end+2:]...) | ||
} else { | ||
res = append(res, intervals[end+1:]...) | ||
} | ||
|
||
return res | ||
} |
This file contains 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,83 @@ | ||
/* | ||
ํ์ด | ||
- DFS | ||
Big O | ||
- N: ๋ ธ๋์ ๊ฐ์ | ||
- H: ํธ๋ฆฌ์ ๋์ด | ||
- Time complexity: O(N) | ||
- ๋ชจ๋ ๋ ธ๋๋ฅผ ํ์ํฉ๋๋ค | ||
- Space complexity: O(H) | ||
- ์ฌ๊ทํธ์ถ ์คํ์ ํฌ๊ธฐ๋ ํธ๋ฆฌ์ ๋์ด์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
func maxDepth(root *TreeNode) int { | ||
maxDepth := 0 | ||
var dig func(*TreeNode, int) | ||
dig = func(node *TreeNode, depth int) { | ||
if node == nil { | ||
if maxDepth < depth { | ||
maxDepth = depth | ||
} | ||
return | ||
} | ||
dig(node.Left, depth+1) | ||
dig(node.Right, depth+1) | ||
} | ||
dig(root, 0) | ||
return maxDepth | ||
} | ||
|
||
/* | ||
ํ์ด | ||
- BFS | ||
Big O | ||
- N: ๋ ธ๋์ ๊ฐ์ | ||
- Time complexity: O(N) | ||
- ๋ชจ๋ ๋ ธ๋๋ฅผ ํ์ํฉ๋๋ค | ||
- Space complexity: O(N) | ||
- ๋ ธ๋ N๊ฐ ์ง๋ฆฌ ํธ๋ฆฌ์์ ํ ์ธต์ ํญ์ N์ ๋์ง ์์ต๋๋ค | ||
๋ฐ๋ผ์ queue์ ๊ณต๊ฐ๋ณต์ก๋๋ O(N)์ ๋๋ค | ||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
func maxDepth(root *TreeNode) int { | ||
level := 0 | ||
queue := make([]*TreeNode, 0) | ||
if root != nil { | ||
queue = append(queue, root) | ||
level++ | ||
} | ||
for len(queue) > 0 { | ||
currQSize := len(queue) | ||
for currQSize > 0 { | ||
node := queue[0] | ||
queue = queue[1:] | ||
currQSize-- | ||
if node.Left != nil { | ||
queue = append(queue, node.Left) | ||
} | ||
if node.Right != nil { | ||
queue = append(queue, node.Right) | ||
} | ||
} | ||
if len(queue) > 0 { | ||
level++ | ||
} | ||
} | ||
return level | ||
} |
This file contains 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 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* type ListNode struct { | ||
* Val int | ||
* Next *ListNode | ||
* } | ||
*/ | ||
func reorderList(head *ListNode) { | ||
// find a middle node | ||
slow, fast := head, head | ||
for fast != nil && fast.Next != nil { | ||
slow = slow.Next | ||
fast = fast.Next.Next | ||
} | ||
// reverse the second part of the list | ||
var prev, curr *ListNode = nil, slow | ||
for curr != nil { | ||
tmp := curr.Next | ||
curr.Next = prev | ||
prev = curr | ||
curr = tmp | ||
} | ||
// merge two parts of the list | ||
for curr1, curr2 := head, prev; curr2.Next != nil; { | ||
tmp1, tmp2 := curr1.Next, curr2.Next | ||
curr1.Next, curr2.Next = curr2, curr1.Next | ||
curr1, curr2 = tmp1, tmp2 | ||
} | ||
} |