-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_RottenOranges.py
More file actions
30 lines (27 loc) · 1015 Bytes
/
Copy pathGraph_RottenOranges.py
File metadata and controls
30 lines (27 loc) · 1015 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
from collections import deque
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
q = deque()
time, fresh = 0, 0
rows, cols = len(grid), len(grid[0])
for i in range(rows):
for j in range(cols):
if grid[i][j] == 1:
fresh += 1
if grid[i][j] == 2:
q.append([i, j])
directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
while q and fresh > 0:
for k in range(len(q)):
i, j = q.popleft()
for r, c in directions:
row, col = i + r, j + c
if (row < 0 or row == len(grid) or
col < 0 or col == len(grid[0]) or
grid[row][col] != 1):
continue
grid[row][col] == 2
q.append([row, col])
fresh -= 1
time += 1
return time if fresh == 0 else -1