-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_matrix.go
170 lines (150 loc) · 3.07 KB
/
01_matrix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package conclusion
// BFS
// Runtime: 236 ms
// Memory Usage: 8.1 MB
func updateMatrix(mat [][]int) [][]int {
if len(mat) == 0 || len(mat[0]) == 0 {
return mat
}
var got = make(map[[2]int]bool)
for i := range mat {
for j := range mat[i] {
mat[i][j] = getClosest(mat, i, j, got)
}
}
return mat
}
func getClosest(mat [][]int, i, j int, got map[[2]int]bool) int {
if mat[i][j] == 0 {
got[[2]int{i, j}] = true
return 0
}
var queue [][2]int
var visited = make(map[[2]int]bool)
var dis int
queue = append(queue, [2]int{i, j})
visited[[2]int{i, j}] = true
R, C := len(mat), len(mat[0])
for len(queue) != 0 {
for k := len(queue); k > 0; k-- {
curr := queue[0]
ii, jj := curr[0], curr[1]
if mat[ii][jj] == 0 {
got[curr] = true
return dis
}
if got[curr] {
return dis + mat[ii][jj]
}
for _, d := range [][2]int{
{0, 1}, // right
{1, 0}, // down
{0, -1}, // left
{-1, 0}, // top
} {
ii, jj := curr[0]+d[0], curr[1]+d[1]
if ii < 0 || ii > R-1 ||
jj < 0 || jj > C-1 {
continue
}
nPos := [2]int{ii, jj}
if visited[nPos] {
continue
}
visited[nPos] = true
queue = append(queue, nPos)
}
queue = queue[1:]
}
dis++
}
return -1
}
// BFS
// Runtime: 108 ms
// Memory Usage: 6.9 MB
func updateMatrix1(mat [][]int) [][]int {
if len(mat) == 0 || len(mat[0]) == 0 {
return mat
}
var rows, cols = len(mat), len(mat[0])
var max = rows + cols
var queue = make([][2]int, 0, rows*cols)
var dist = make([][]int, rows)
// put all 0 in queue
for i := range mat {
dist[i] = make([]int, cols)
for j := range mat[i] {
if mat[i][j] == 0 {
dist[i][j] = 0
queue = append(queue, [2]int{i, j})
} else {
dist[i][j] = max
}
}
}
for len(queue) != 0 {
curr := queue[0]
queue = queue[1:]
for _, d := range [][2]int{
{0, 1}, // right
{1, 0}, // down
{0, -1}, // left
{-1, 0}, // top
} {
nr, nc := curr[0]+d[0], curr[1]+d[1]
if nr < 0 || nr > rows-1 ||
nc < 0 || nc > cols-1 {
continue
}
if ndist := dist[curr[0]][curr[1]] + 1; dist[nr][nc] > ndist {
dist[nr][nc] = ndist
queue = append(queue, [2]int{nr, nc})
}
}
}
return dist
}
// DP
// Runtime: 84 ms
// Memory Usage: 6.8 MB
func updateMatrix2(mat [][]int) [][]int {
rows := len(mat)
if rows == 0 {
return mat
}
cols := len(mat[0])
if cols == 0 {
return mat
}
var dist = make([][]int, rows)
// check for left and top
for i := range mat {
dist[i] = make([]int, cols)
for j := range mat[i] {
if mat[i][j] == 0 {
dist[i][j] = 0
} else {
dist[i][j] = rows + cols
if i > 0 && dist[i][j] > dist[i-1][j]+1 {
dist[i][j] = dist[i-1][j] + 1
}
if j > 0 && dist[i][j] > dist[i][j-1]+1 {
dist[i][j] = dist[i][j-1] + 1
}
}
}
}
// check for right and bottom
for i := rows - 1; i >= 0; i-- {
for j := cols - 1; j >= 0; j-- {
if i < rows-1 && dist[i][j] > dist[i+1][j]+1 {
dist[i][j] = dist[i+1][j] + 1
}
if j < cols-1 && dist[i][j] > dist[i][j+1]+1 {
dist[i][j] = dist[i][j+1] + 1
}
}
}
return dist
}