-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path542.go
More file actions
49 lines (46 loc) · 870 Bytes
/
542.go
File metadata and controls
49 lines (46 loc) · 870 Bytes
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
func min(x int, y int) int {
if x < y {
return x
} else {
return y
}
}
func updateMatrix(mat [][]int) [][]int {
res := [][]int{}
for _, element := range mat {
tmp := []int{}
for _, v := range element {
if v == 0 {
tmp = append(tmp, v)
} else {
tmp = append(tmp, math.MaxInt-1)
}
}
res = append(res, tmp)
}
for i := 0; i < len(mat); i++ {
for j := 0; j < len(mat[i]); j++ {
if mat[i][j] != 0 {
if i > 0 {
res[i][j] = min(res[i][j], res[i-1][j]+1)
}
if j > 0 {
res[i][j] = min(res[i][j], res[i][j-1]+1)
}
}
}
}
for i := len(mat) - 1; i >= 0; i-- {
for j := len(mat[i]) - 1; j >= 0; j-- {
if mat[i][j] != 0 {
if i < len(mat)-1 {
res[i][j] = min(res[i][j], res[i+1][j]+1)
}
if j < len(mat[i])-1 {
res[i][j] = min(res[i][j], res[i][j+1]+1)
}
}
}
}
return res
}