-
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 #565 from obzva/main
[Flynn] Week 12
- Loading branch information
Showing
5 changed files
with
258 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,41 @@ | ||
/* | ||
Big O | ||
- N: ์ฃผ์ด์ง ๋ฐฐ์ด intervals์ ๊ธธ์ด | ||
- Time complexity: O(NlogN) | ||
- intervals๋ฅผ start์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ -> O(NlogN) | ||
- ๋ฐ๋ณต๋ฌธ -> O(N) | ||
- O(NlogN + N) = O(NlogN) | ||
- Space complexity: O(N) | ||
- ์ ๋ต ๋ฐฐ์ด์ ํฌ๊ธฐ -> O(N) | ||
*/ | ||
|
||
import "sort" | ||
|
||
func merge(intervals [][]int) [][]int { | ||
sort.Slice(intervals, func(i, j int) bool { | ||
return intervals[i][0] < intervals[j][0] | ||
}) | ||
res := make([][]int, 0) | ||
start := intervals[0][0] | ||
end := intervals[0][1] | ||
for i := 1; i < len(intervals); i++ { | ||
curr := intervals[i] | ||
if end >= curr[0] { | ||
end = max(end, curr[1]) | ||
} else { | ||
res = append(res, []int{start, end}) | ||
start = curr[0] | ||
end = curr[1] | ||
} | ||
} | ||
res = append(res, []int{start, end}) | ||
return res | ||
} | ||
|
||
func max(a, b int) int { | ||
if a > b { | ||
return a | ||
} else { | ||
return b | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
number-of-connected-components-in-an-undirected-graph/flynn.go
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,47 @@ | ||
/* | ||
ํ์ด | ||
- DFS์ hashmap(set)์ ์ด์ฉํ์ฌ ํ์ดํ ์ ์์ต๋๋ค | ||
- ์ด์ ์ ํ์ดํ๋ course schedule ๋ฌธ์ ์ ์ ์ฌํฉ๋๋ค | ||
Big O | ||
- N: ๋ ธ๋ ๊ฐ์ | ||
- E: ๊ฐ์ ์ ๊ฐ์ | ||
- Time complexity: O(N + E) | ||
- adj๋ฅผ ์์ฑํ๋ ๋ฐ๋ณต๋ฌธ์ ์๊ฐ๋ณต์ก๋๋ E์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
- ์ ์ฒด ๋ ธ๋๋ฅผ ์ต๋ 1๋ฒ์ฉ ์กฐํํ๋ฏ๋ก ๋๋ฒ์งธ ๋ฐ๋ณต๋ฌธ์ ์๊ฐ๋ณต์ก๋๋ N์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
- Space complexity: O(N + E) | ||
- adjacency list์ ํฌ๊ธฐ๋ E์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
- checked์ ํฌ๊ธฐ๋ N์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
- check ํจ์์ ์ฌ๊ท ํธ์ถ ์คํ ๊น์ด ๋ํ ์ต์ ์ ๊ฒฝ์ฐ, N์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
*/ | ||
|
||
func countComponents(n int, edges [][]int) int { | ||
adj := make(map[int][]int) | ||
for _, edge := range edges { | ||
adj[edge[0]] = append(adj[edge[0]], edge[1]) | ||
adj[edge[1]] = append(adj[edge[1]], edge[0]) | ||
} | ||
// Go๋ {int: bool} hashmap์ set์ฒ๋ผ ์ฌ์ฉํจ | ||
checked := make(map[int]bool) // ๋ชจ๋ ํ์์ด ๋๋ ๋ ธ๋๋ฅผ ๊ธฐ๋กํจ | ||
// ๊ฐ node๋ฅผ ์กฐํํ๋ ํจ์ | ||
var check func(int) | ||
check = func(i int) { | ||
checked[i] = true | ||
for _, nxt := range adj[i] { | ||
if _, ok := checked[nxt]; ok { | ||
continue | ||
} | ||
check(nxt) | ||
} | ||
} | ||
|
||
res := 0 | ||
for i := 0; i < n; i++ { | ||
if _, ok := checked[i]; ok { | ||
continue | ||
} | ||
res++ | ||
check(i) | ||
} | ||
|
||
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,34 @@ | ||
/* | ||
ํ์ด | ||
- n+1 ๊ฐ๊ฒฉ์ ์ ์งํ๋ฉฐ ์ด๋ํ๋ ๋ ๊ฐ์ ํฌ์ธํฐ๋ฅผ ์ด์ฉํ๋ฉด one-pass๋ก ํด๊ฒฐํ ์ ์์ต๋๋ค | ||
Big O | ||
- M: ๋งํฌ๋๋ฆฌ์คํธ์ ๊ธธ์ด | ||
- Time complexity: O(M) | ||
- Space complexity: O(1) | ||
*/ | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* type ListNode struct { | ||
* Val int | ||
* Next *ListNode | ||
* } | ||
*/ | ||
|
||
func removeNthFromEnd(head *ListNode, n int) *ListNode { | ||
dummy := &ListNode{Next: head} | ||
|
||
slow := dummy | ||
fast := dummy | ||
for i := 0; i < n+1; i++ { | ||
fast = fast.Next | ||
} | ||
|
||
for fast != nil { | ||
slow = slow.Next | ||
fast = fast.Next | ||
} | ||
slow.Next = slow.Next.Next | ||
|
||
return dummy.Next | ||
} |
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,38 @@ | ||
/* | ||
ํ์ด | ||
- ์ฌ๊ทํจ์๋ฅผ ์ด์ฉํด์ ํ์ดํ ์ ์์ต๋๋ค | ||
Big O | ||
- N: ํธ๋ฆฌ ๋ ธ๋์ ๊ฐ์ | ||
- H: ํธ๋ฆฌ์ ๋์ด (logN <= H <= N) | ||
- Time complexity: O(N) | ||
- ๋ชจ๋ ๋ ธ๋๋ฅผ ์ต๋ 1๋ฒ ํ์ํฉ๋๋ค | ||
- Space complexity: O(H) | ||
- ์ฌ๊ท ํธ์ถ ์คํ์ ๊น์ด๋ H์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
*/ | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
func isSameTree(p *TreeNode, q *TreeNode) bool { | ||
// base case | ||
if p == nil && q == nil { | ||
return true | ||
} else if p == nil || q == nil { | ||
return false | ||
} | ||
|
||
if p.Val != q.Val { | ||
return false | ||
} | ||
|
||
if !isSameTree(p.Left, q.Left) || !isSameTree(p.Right, q.Right) { | ||
return false | ||
} | ||
|
||
return true | ||
} |
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,98 @@ | ||
/* | ||
ํ์ด | ||
- DFS๋ฅผ ์ด์ฉํ์ฌ ํ์ดํฉ๋๋ค | ||
Big O | ||
- N: ๋ ธ๋์ ์ | ||
- Serialize | ||
- Time complexity: O(N) | ||
- ๋ชจ๋ ๋ ธ๋๋ฅผ ์ต๋ 1๋ฒ ์กฐํํฉ๋๋ค | ||
- Space complexity: O(N) | ||
- buildString์ ์ฌ๊ท ํธ์ถ ์คํ์ ๊น์ด๋ ๋ ธ๋์ ๋์ด์ ๋น๋กํ์ฌ ์ฆ๊ฐํ๋ฉฐ, ๋ ธ๋์ ๋์ด๋ ์ต๋ N์ ๋๋ค | ||
- ๊ฒฐ๊ณผ string์ ํฌ๊ธฐ ๋ํ N์ ๋น๋กํ๋ ํํ๋ก ์ฆ๊ฐํฉ๋๋ค | ||
- Deserialize | ||
- Time complexity: O(N) | ||
- ๋ชจ๋ ๋ ธ๋๋ฅผ ์ต๋ 1๋ฒ ์กฐํํฉ๋๋ค | ||
- Space complexity: O(N) | ||
- data๋ฅผ splitํ ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ N์ ๋น๋กํ์ฌ ์ฆ๊ฐํฉ๋๋ค | ||
- buildTree์ ์ฌ๊ท ํธ์ถ ์คํ์ ๊น์ด๋ ๋ ธ๋์ ๋์ด์ ๋น๋กํ์ฌ ์ฆ๊ฐํ๋ฉฐ, ๋ ธ๋์ ๋์ด๋ ์ต๋ N์ ๋๋ค | ||
*/ | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* type TreeNode struct { | ||
* Val int | ||
* Left *TreeNode | ||
* Right *TreeNode | ||
* } | ||
*/ | ||
const ( | ||
DELIMITER = "|" | ||
) | ||
|
||
type Codec struct { | ||
} | ||
|
||
func Constructor() Codec { | ||
codec := Codec{} | ||
return codec | ||
} | ||
|
||
// Serializes a tree to a single string. | ||
func (this *Codec) serialize(root *TreeNode) string { | ||
if root == nil { | ||
return "" | ||
} | ||
var sb strings.Builder | ||
buildString(&sb, root) | ||
return sb.String() | ||
} | ||
|
||
// Deserializes your encoded data to tree. | ||
func (this *Codec) deserialize(data string) *TreeNode { | ||
if data == "" { | ||
return nil | ||
} | ||
splitData := make([]string, 0, len(data)/2) | ||
splitData = strings.Split(data, DELIMITER) | ||
splitData = splitData[:len(splitData)-1] | ||
return buildTree(&splitData) | ||
} | ||
|
||
// ----- Helpers ----- | ||
func buildString(sb *strings.Builder, node *TreeNode) { | ||
if node == nil { | ||
sb.WriteString(DELIMITER) | ||
return | ||
} | ||
sb.WriteString(strconv.Itoa(node.Val)) | ||
sb.WriteString(DELIMITER) | ||
buildString(sb, node.Left) | ||
buildString(sb, node.Right) | ||
} | ||
|
||
func buildTree(splitData *[]string) *TreeNode { | ||
val := (*splitData)[0] | ||
*splitData = (*splitData)[1:] | ||
if val == "" { | ||
return nil | ||
} | ||
node := &TreeNode{} | ||
intVal, _ := strconv.Atoi(val) | ||
node.Val = intVal | ||
node.Left = buildTree(splitData) | ||
node.Right = buildTree(splitData) | ||
return node | ||
} | ||
|
||
/** | ||
* Your Codec object will be instantiated and called as such: | ||
* ser := Constructor(); | ||
* deser := Constructor(); | ||
* data := ser.serialize(root); | ||
* ans := deser.deserialize(data); | ||
*/ |