-
Notifications
You must be signed in to change notification settings - Fork 1
/
LC994_RottenOranges.py
50 lines (35 loc) · 1.45 KB
/
LC994_RottenOranges.py
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
from collections import deque
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
'''
Add all rotten oranges to q
do a bfs, track time
return time
'''
def getNeighbors(r, c):
ans = []
delta = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for x, y in delta:
new_r = r + x
new_c = c + y
if 0 <= new_r < len(grid) and 0 <= new_c < len(grid[0]) and grid[new_r][new_c] == 1:
ans.append((new_r, new_c))
return ans
q = deque()
total_unrotten = 0
#visited = set() -> Do not really need a visited set as I can change the input grid
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 2:
q.append((i, j, 0))
elif grid[i][j] == 1:
total_unrotten += 1
curr_time = 0
while q:
r, c, curr_time = q.popleft()
neighbors = getNeighbors(r, c)
for row, col in neighbors:
grid[row][col] = 2
total_unrotten -= 1
q.append((row, col, curr_time + 1))
return curr_time if total_unrotten == 0 else -1