-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path01_matrix.go
57 lines (51 loc) · 946 Bytes
/
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
package main
func updateMatrix(mat [][]int) [][]int {
n := len(mat)
m := len(mat[0])
if mat[0][0] != 0 {
mat[0][0] = m + n
}
// Travel top down
for j := 1; j < m; j++ {
if mat[0][j] != 0 {
mat[0][j] = mat[0][j-1] + 1
}
}
for i := 1; i < n; i++ {
if mat[i][0] != 0 {
mat[i][0] = mat[i-1][0] + 1
}
}
for i := 1; i < n; i++ {
for j := 1; j < m; j++ {
if mat[i][j] != 0 {
mat[i][j] = min(mat[i-1][j], mat[i][j-1]) + 1
}
}
}
// Travel up
for j := m - 2; j >= 0; j-- {
if mat[n-1][j] != 0 {
mat[n-1][j] = min(mat[n-1][j], mat[n-1][j+1]+1)
}
}
for i := n - 2; i >= 0; i-- {
if mat[i][m-1] != 0 {
mat[i][m-1] = min(mat[i][m-1], mat[i+1][m-1]+1)
}
}
for i := n - 2; i >= 0; i-- {
for j := m - 2; j >= 0; j-- {
if mat[i][j] != 0 {
mat[i][j] = min(mat[i][j], min(mat[i+1][j], mat[i][j+1])+1)
}
}
}
return mat
}
func min(a, b int) int {
if a < b {
return a
}
return b
}