-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-matrix.go
86 lines (69 loc) · 1.37 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
package main
import (
"fmt"
"math"
)
// source: https://leetcode.com/problems/01-matrix/
type queue struct {
val [][2]int
}
func newQueue() queue {
return queue{
val: make([][2]int, 0),
}
}
func (q *queue) push(i, j int) {
q.val = append(q.val, [2]int{i, j})
}
func (q *queue) pop() (int, int) {
i, j := q.val[0][0], q.val[0][1]
q.val = q.val[1:]
return i, j
}
func (q *queue) len() int {
return len(q.val)
}
func updateMatrix(mat [][]int) [][]int {
n, m := len(mat), len(mat[0])
mem := make([][]int, n)
q := newQueue()
for i := range mem {
mem[i] = make([]int, m)
for j := range mem[i] {
if mat[i][j] == 0 {
q.push(i, j)
} else {
mem[i][j] = math.MaxInt
}
}
}
for q.len() > 0 {
i, j := q.pop()
v := mem[i][j] + 1
if i-1 >= 0 && mem[i-1][j] > v {
mem[i-1][j] = v
q.push(i-1, j)
}
if i+1 < n && mem[i+1][j] > v {
mem[i+1][j] = v
q.push(i+1, j)
}
if j-1 >= 0 && mem[i][j-1] > v {
mem[i][j-1] = v
q.push(i, j-1)
}
if j+1 < m && mem[i][j+1] > v {
mem[i][j+1] = v
q.push(i, j+1)
}
}
return mem
}
func main() {
// Example 1
var mat1 = [][]int{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}
fmt.Println("Expected: [[0,0,0],[0,1,0],[0,0,0]] Output: ", updateMatrix(mat1))
// Example 2
var mat2 = [][]int{{0, 0, 0}, {0, 1, 0}, {1, 1, 1}}
fmt.Println("Expected: [[0,0,0],[0,1,0],[1,2,1]] Output: ", updateMatrix(mat2))
}