-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0994-rotting-oranges.go
48 lines (43 loc) · 1.22 KB
/
0994-rotting-oranges.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
const ROW = 0
const COL = 1
func orangesRotting(grid [][]int) int {
q := make([][]int, 0)
fresh := 0
time := 0
for r := 0; r < len(grid); r++ {
for c := 0; c < len(grid[0]); c++ {
if grid[r][c] == 1 {
fresh += 1
}
if grid[r][c] == 2 {
q = append(q, []int{r, c})
}
}
}
directions := [4][2]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
for fresh > 0 && len(q) != 0 {
length := len(q)
for i := 0; i < length; i++ {
cell := q[0]
q = q[1:]
for _, d := range directions {
row, col := cell[ROW] + d[ROW], cell[COL] + d[COL]
// if in bounds and notrotten, make rotten
// and add to q
if(
row >= 0 && row < len(grid) &&
col >= 0 && col < len(grid[0]) &&
grid[row][col] == 1) {
grid[row][col] = 2
q = append(q, []int{row, col})
fresh -= 1
}
}
}
time += 1
}
if fresh == 0 {
return time;
}
return -1
}