Skip to content

Commit fa89896

Browse files
authored
Merge pull request #565 from obzva/main
[Flynn] Week 12
2 parents c70fa10 + cde1132 commit fa89896

File tree

5 files changed

+258
-0
lines changed

5 files changed

+258
-0
lines changed

merge-intervals/flynn.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Big O
3+
- N: 주어진 배열 intervals의 길이
4+
- Time complexity: O(NlogN)
5+
- intervals를 start의 오름차순으로 정렬 -> O(NlogN)
6+
- 반복문 -> O(N)
7+
- O(NlogN + N) = O(NlogN)
8+
- Space complexity: O(N)
9+
- 정답 배열의 크기 -> O(N)
10+
*/
11+
12+
import "sort"
13+
14+
func merge(intervals [][]int) [][]int {
15+
sort.Slice(intervals, func(i, j int) bool {
16+
return intervals[i][0] < intervals[j][0]
17+
})
18+
res := make([][]int, 0)
19+
start := intervals[0][0]
20+
end := intervals[0][1]
21+
for i := 1; i < len(intervals); i++ {
22+
curr := intervals[i]
23+
if end >= curr[0] {
24+
end = max(end, curr[1])
25+
} else {
26+
res = append(res, []int{start, end})
27+
start = curr[0]
28+
end = curr[1]
29+
}
30+
}
31+
res = append(res, []int{start, end})
32+
return res
33+
}
34+
35+
func max(a, b int) int {
36+
if a > b {
37+
return a
38+
} else {
39+
return b
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
풀이
3+
- DFS와 hashmap(set)을 이용하여 풀이할 수 있습니다
4+
- 이전에 풀이했던 course schedule 문제와 유사합니다
5+
Big O
6+
- N: 노드 개수
7+
- E: 간선의 개수
8+
- Time complexity: O(N + E)
9+
- adj를 생성하는 반복문의 시간복잡도는 E에 비례하여 증가합니다
10+
- 전체 노드를 최대 1번씩 조회하므로 두번째 반복문의 시간복잡도는 N에 비례하여 증가합니다
11+
- Space complexity: O(N + E)
12+
- adjacency list의 크기는 E에 비례하여 증가합니다
13+
- checked의 크기는 N에 비례하여 증가합니다
14+
- check 함수의 재귀 호출 스택 깊이 또한 최악의 경우, N에 비례하여 증가합니다
15+
*/
16+
17+
func countComponents(n int, edges [][]int) int {
18+
adj := make(map[int][]int)
19+
for _, edge := range edges {
20+
adj[edge[0]] = append(adj[edge[0]], edge[1])
21+
adj[edge[1]] = append(adj[edge[1]], edge[0])
22+
}
23+
// Go는 {int: bool} hashmap을 set처럼 사용함
24+
checked := make(map[int]bool) // 모든 탐색이 끝난 노드를 기록함
25+
// 각 node를 조회하는 함수
26+
var check func(int)
27+
check = func(i int) {
28+
checked[i] = true
29+
for _, nxt := range adj[i] {
30+
if _, ok := checked[nxt]; ok {
31+
continue
32+
}
33+
check(nxt)
34+
}
35+
}
36+
37+
res := 0
38+
for i := 0; i < n; i++ {
39+
if _, ok := checked[i]; ok {
40+
continue
41+
}
42+
res++
43+
check(i)
44+
}
45+
46+
return res
47+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
풀이
3+
- n+1 간격을 유지하며 이동하는 두 개의 포인터를 이용하면 one-pass로 해결할 수 있습니다
4+
Big O
5+
- M: 링크드리스트의 길이
6+
- Time complexity: O(M)
7+
- Space complexity: O(1)
8+
*/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* type ListNode struct {
13+
* Val int
14+
* Next *ListNode
15+
* }
16+
*/
17+
18+
func removeNthFromEnd(head *ListNode, n int) *ListNode {
19+
dummy := &ListNode{Next: head}
20+
21+
slow := dummy
22+
fast := dummy
23+
for i := 0; i < n+1; i++ {
24+
fast = fast.Next
25+
}
26+
27+
for fast != nil {
28+
slow = slow.Next
29+
fast = fast.Next
30+
}
31+
slow.Next = slow.Next.Next
32+
33+
return dummy.Next
34+
}

same-tree/flynn.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
풀이
3+
- 재귀함수를 이용해서 풀이할 수 있습니다
4+
Big O
5+
- N: 트리 노드의 개수
6+
- H: 트리의 높이 (logN <= H <= N)
7+
- Time complexity: O(N)
8+
- 모든 노드를 최대 1번 탐색합니다
9+
- Space complexity: O(H)
10+
- 재귀 호출 스택의 깊이는 H에 비례하여 증가합니다
11+
*/
12+
13+
/**
14+
* Definition for a binary tree node.
15+
* type TreeNode struct {
16+
* Val int
17+
* Left *TreeNode
18+
* Right *TreeNode
19+
* }
20+
*/
21+
func isSameTree(p *TreeNode, q *TreeNode) bool {
22+
// base case
23+
if p == nil && q == nil {
24+
return true
25+
} else if p == nil || q == nil {
26+
return false
27+
}
28+
29+
if p.Val != q.Val {
30+
return false
31+
}
32+
33+
if !isSameTree(p.Left, q.Left) || !isSameTree(p.Right, q.Right) {
34+
return false
35+
}
36+
37+
return true
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
풀이
3+
- DFS를 이용하여 풀이합니다
4+
Big O
5+
- N: 노드의 수
6+
- Serialize
7+
- Time complexity: O(N)
8+
- 모든 노드를 최대 1번 조회합니다
9+
- Space complexity: O(N)
10+
- buildString의 재귀 호출 스택의 깊이는 노드의 높이에 비례하여 증가하며, 노드의 높이는 최대 N입니다
11+
- 결과 string의 크기 또한 N에 비례하는 형태로 증가합니다
12+
- Deserialize
13+
- Time complexity: O(N)
14+
- 모든 노드를 최대 1번 조회합니다
15+
- Space complexity: O(N)
16+
- data를 split한 배열의 크기가 N에 비례하여 증가합니다
17+
- buildTree의 재귀 호출 스택의 깊이는 노드의 높이에 비례하여 증가하며, 노드의 높이는 최대 N입니다
18+
*/
19+
20+
import (
21+
"strconv"
22+
"strings"
23+
)
24+
25+
/**
26+
* Definition for a binary tree node.
27+
* type TreeNode struct {
28+
* Val int
29+
* Left *TreeNode
30+
* Right *TreeNode
31+
* }
32+
*/
33+
const (
34+
DELIMITER = "|"
35+
)
36+
37+
type Codec struct {
38+
}
39+
40+
func Constructor() Codec {
41+
codec := Codec{}
42+
return codec
43+
}
44+
45+
// Serializes a tree to a single string.
46+
func (this *Codec) serialize(root *TreeNode) string {
47+
if root == nil {
48+
return ""
49+
}
50+
var sb strings.Builder
51+
buildString(&sb, root)
52+
return sb.String()
53+
}
54+
55+
// Deserializes your encoded data to tree.
56+
func (this *Codec) deserialize(data string) *TreeNode {
57+
if data == "" {
58+
return nil
59+
}
60+
splitData := make([]string, 0, len(data)/2)
61+
splitData = strings.Split(data, DELIMITER)
62+
splitData = splitData[:len(splitData)-1]
63+
return buildTree(&splitData)
64+
}
65+
66+
// ----- Helpers -----
67+
func buildString(sb *strings.Builder, node *TreeNode) {
68+
if node == nil {
69+
sb.WriteString(DELIMITER)
70+
return
71+
}
72+
sb.WriteString(strconv.Itoa(node.Val))
73+
sb.WriteString(DELIMITER)
74+
buildString(sb, node.Left)
75+
buildString(sb, node.Right)
76+
}
77+
78+
func buildTree(splitData *[]string) *TreeNode {
79+
val := (*splitData)[0]
80+
*splitData = (*splitData)[1:]
81+
if val == "" {
82+
return nil
83+
}
84+
node := &TreeNode{}
85+
intVal, _ := strconv.Atoi(val)
86+
node.Val = intVal
87+
node.Left = buildTree(splitData)
88+
node.Right = buildTree(splitData)
89+
return node
90+
}
91+
92+
/**
93+
* Your Codec object will be instantiated and called as such:
94+
* ser := Constructor();
95+
* deser := Constructor();
96+
* data := ser.serialize(root);
97+
* ans := deser.deserialize(data);
98+
*/

0 commit comments

Comments
 (0)