Skip to content

Commit 93e3a0d

Browse files
committed
solve: week 09
1 parent 8cf1244 commit 93e3a0d

File tree

5 files changed

+246
-0
lines changed

5 files changed

+246
-0
lines changed

clone-graph/invidam.go.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Complexity
2+
- Time Complexity: $O(V+E)$
3+
- 정점의 수 V와 간선의 수 E에 대해, 모든 정점의 수 만큼 재귀호출이 일어거나 모든 간선의 수 만큼 반복문이 실행되므로 `V+E` 이다.
4+
- Space Complexity: $O(V+E)$
5+
- 정점의 수 V와 간선의 수 E에 대해, 모든 정점의 수 만큼 복제본들을 저장하는 배열(`copied`)가 선언되고 모든 간선의 수 만큼 배열(`Neighbors`)가 선언되므로 `V+E` 이다.
6+
7+
# Code
8+
```go
9+
func deepCopy(node *Node, copied map[int]*Node) *Node {
10+
if node == nil {
11+
return nil
12+
}
13+
copied[node.Val] = &Node{
14+
Val: node.Val,
15+
Neighbors: make([]*Node, len(node.Neighbors)),
16+
}
17+
18+
for i := 0; i < len(node.Neighbors); i++ {
19+
if c, found := copied[node.Neighbors[i].Val]; found {
20+
copied[node.Val].Neighbors[i] = c
21+
continue
22+
}
23+
copied[node.Val].Neighbors[i] = deepCopy(node.Neighbors[i], copied)
24+
}
25+
26+
return copied[node.Val]
27+
}
28+
29+
func cloneGraph(node *Node) *Node {
30+
return deepCopy(node, make(map[int]*Node, 0))
31+
}
32+
33+
```

course-schedule/invidam.go.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Complexity
2+
- Time Complexity: $O(R*C)$
3+
- 정점 수와 간선 수인 V와 E에 대해, dfs인 `hasCycle`을 모두 호출하는 데 비용 V가 발생하고 반복문을 모두 순회하는 데 비용 E가 발생하여 총 비용 `V+E`가 발생한다.
4+
- Space Complexity: $O(V+E)$
5+
- 정점 수와 간선 수인 V와 E에 대해, 인접 리스트 `adj`을 선언하는 데 비용 `V+E`가 발생한다.
6+
7+
# Code
8+
```go
9+
func hasCycle(curr int, adj [][]int, visited []bool, finish []bool) bool {
10+
if visited[curr] {
11+
return !finish[curr]
12+
}
13+
visited[curr] = true
14+
15+
for _, next := range adj[curr] {
16+
if hasCycle(next, adj, visited, finish) {
17+
return true
18+
}
19+
}
20+
finish[curr] = true
21+
22+
return false
23+
}
24+
25+
func canFinish(numCourses int, prerequisites [][]int) bool {
26+
adj := make([][]int, numCourses)
27+
for i, _ := range adj {
28+
adj[i] = make([]int, 0)
29+
}
30+
for _, prerequisite := range prerequisites {
31+
from, to := prerequisite[0], prerequisite[1]
32+
adj[from] = append(adj[from], to)
33+
}
34+
35+
visited := make([]bool, numCourses)
36+
finish := make([]bool, numCourses)
37+
for from := range adj {
38+
if hasCycle(from, adj, visited, finish) {
39+
return false
40+
}
41+
}
42+
return true
43+
}
44+
45+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Complexity
2+
- AddWord
3+
- Time Complexity: $O(n)$
4+
- 단어의 길이 n만큼 재귀호출이 일어난다.
5+
- Space Complexity: $O(n)$
6+
- 단어의 길이 n만큼 재귀호출이 일어난다.
7+
- Search
8+
- Time Complexity: $O(n)$
9+
- 단어의 길이 n만큼 재귀호출이 일어난다.
10+
- Space Complexity: $O(n)$
11+
- 단어의 길이 n만큼 재귀호출이 일어난다.
12+
- `.`의 개수, 단어의 범위는 모두 2, 26개로 상수개라 제외했다.
13+
14+
15+
# Code
16+
17+
```go
18+
type WordDictionary struct {
19+
Nodes []*WordDictionary
20+
IsTerminated bool
21+
}
22+
23+
func Constructor() WordDictionary {
24+
return WordDictionary{Nodes: make([]*WordDictionary, 26)}
25+
}
26+
27+
func (this *WordDictionary) AddWord(word string) {
28+
if len(word) == 0 {
29+
this.IsTerminated = true
30+
return
31+
}
32+
33+
if this.Nodes[word[0]-'a'] == nil {
34+
newNode := Constructor()
35+
this.Nodes[word[0]-'a'] = &newNode
36+
37+
}
38+
this.Nodes[word[0]-'a'].AddWord(word[1:])
39+
}
40+
41+
func (this *WordDictionary) Search(word string) bool {
42+
if len(word) == 0 {
43+
return this.IsTerminated
44+
}
45+
46+
if word[0] == '.' {
47+
for c := 'a'; c <= 'z'; c++ {
48+
if this.Search(string(c) + word[1:]) {
49+
return true
50+
}
51+
}
52+
return false
53+
}
54+
55+
if this.Nodes[word[0]-'a'] == nil {
56+
return false
57+
}
58+
return this.Nodes[word[0]-'a'].Search(word[1:])
59+
}
60+
61+
```

number-of-islands/invidam.go.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Complexity
2+
- Time Complexity: $O(M*N)$
3+
- 모든 섬을 방문하는 경우가 최대이며, 이 때 `grid`의 행의 길이인 M, 열의 길이인 N을 곱한 M*N만큼 재귀호출이 일어난다.
4+
- Space Complexity: $O(M*N)$
5+
- 모든 섬의 방문 여부를 저장하기 위해 `grid`의 행의 길이인 M, 열의 길이인 N을 곱한 M*N 크기의 `visited` 배열을 선언했다.
6+
7+
# Code
8+
```go
9+
var offsets = [][]int{
10+
{1, 0},
11+
{-1, 0},
12+
{0, 1},
13+
{0, -1},
14+
}
15+
16+
func update(i int, j int, grid [][]byte, visited [][]bool) {
17+
visited[i][j] = true
18+
for _, offset := range offsets {
19+
ni, nj := i+offset[0], j+offset[1]
20+
if ni < 0 || ni >= len(grid) || nj < 0 || nj >= len(grid[0]) || visited[ni][nj] || grid[ni][nj] == '0' {
21+
continue
22+
}
23+
update(ni, nj, grid, visited)
24+
}
25+
}
26+
27+
func numIslands(grid [][]byte) int {
28+
visited := make([][]bool, len(grid))
29+
for i, _ := range visited {
30+
visited[i] = make([]bool, len(grid[0]))
31+
}
32+
33+
var cnt int
34+
for i, row := range grid {
35+
for j, val := range row {
36+
if val == '0' || visited[i][j] {
37+
continue
38+
}
39+
update(i, j, grid, visited)
40+
cnt++
41+
}
42+
}
43+
return cnt
44+
}
45+
46+
```
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Complexity
2+
- Time Complexity: $O(R*C)$
3+
- `heights`의 행과 열인 R과 C에 대해, 반복문과 DFS의 비용인 `R*C`가 발생한다.
4+
- Space Complexity: $O(R*C)$
5+
- `heights`의 행과 열인 R과 C에 대해, 방문 여부를 기록하는 배열(`pacifics`, `atlantics`)의 크기 비용인 `R*C`가 발생한다.
6+
7+
# Code
8+
```go
9+
var offsets = [][]int{
10+
{1, 0},
11+
{-1, 0},
12+
{0, 1},
13+
{0, -1},
14+
}
15+
16+
func update(i int, j int, heights [][]int, visited [][]bool) {
17+
visited[i][j] = true
18+
19+
for _, offset := range offsets {
20+
ni, nj := i+offset[0], j+offset[1]
21+
22+
if ni < 0 || ni >= len(visited) || nj < 0 || nj >= len(visited[0]) || visited[ni][nj] || heights[i][j] > heights[ni][nj] {
23+
continue
24+
}
25+
update(ni, nj, heights, visited)
26+
}
27+
}
28+
29+
func pacificAtlantic(heights [][]int) [][]int {
30+
parcifics := make([][]bool, len(heights))
31+
for i, _ := range parcifics {
32+
parcifics[i] = make([]bool, len(heights[0]))
33+
}
34+
atlantics := make([][]bool, len(heights))
35+
for i, _ := range atlantics {
36+
atlantics[i] = make([]bool, len(heights[0]))
37+
}
38+
39+
for i, _ := range heights {
40+
for j, _ := range heights[0] {
41+
if i == 0 || j == 0 {
42+
update(i, j, heights, parcifics)
43+
}
44+
if i == len(heights)-1 || j == len(heights[0])-1 {
45+
update(i, j, heights, atlantics)
46+
}
47+
}
48+
}
49+
boths := make([][]int, 0)
50+
for i, _ := range heights {
51+
for j, _ := range heights[0] {
52+
if !(parcifics[i][j] && atlantics[i][j]) {
53+
continue
54+
}
55+
boths = append(boths, []int{i, j})
56+
}
57+
}
58+
return boths
59+
}
60+
61+
```

0 commit comments

Comments
 (0)