-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
74 lines (67 loc) · 1.38 KB
/
solution.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
package rotting_oranges
type point struct {
x int
y int
}
func OrangesRotting(grid [][]int) int {
if len(grid) == 0 || len(grid[0]) == 0 {
return 0
}
rottenOranges := findRottenOranges(grid)
runs := -1
for len(rottenOranges) > 0 {
rottenOranges = rot(grid, rottenOranges)
runs++
}
if hasFreshOranges(grid) {
return -1
} else if runs < 0 {
return 0
} else {
return runs
}
}
func findRottenOranges(grid [][]int) []point {
var result []point
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[0]); j++ {
if grid[i][j] == 2 {
result = append(result, point{i, j})
}
}
}
return result
}
func rot(grid [][]int, rotten []point) []point {
m, n := len(grid), len(grid[0])
var result []point
for _, p := range rotten {
if p.x > 0 && grid[p.x-1][p.y] == 1 {
result = append(result, point{p.x - 1, p.y})
grid[p.x-1][p.y] = 2
}
if p.x < m-1 && grid[p.x+1][p.y] == 1 {
result = append(result, point{p.x + 1, p.y})
grid[p.x+1][p.y] = 2
}
if p.y > 0 && grid[p.x][p.y-1] == 1 {
result = append(result, point{p.x, p.y - 1})
grid[p.x][p.y-1] = 2
}
if p.y < n-1 && grid[p.x][p.y+1] == 1 {
result = append(result, point{p.x, p.y + 1})
grid[p.x][p.y+1] = 2
}
}
return result
}
func hasFreshOranges(grid [][]int) bool {
for _, row := range grid {
for _, cell := range row {
if cell == 1 {
return true
}
}
}
return false
}