-
Notifications
You must be signed in to change notification settings - Fork 0
/
ROTTEN_ORANGES_LEETCODE.py
43 lines (36 loc) · 1.49 KB
/
ROTTEN_ORANGES_LEETCODE.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
from queue import Queue
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
que = Queue()
ans = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 2:
que.put([i,j, 0])
while not que.empty():
curr = que.get()
level = curr[2]
# top
if ((curr[0] - 1 )>= 0) and (grid[curr[0]-1][curr[1]] == 1):
grid[curr[0]-1][curr[1]]=2
que.put([curr[0]-1, curr[1], level+1])
# bottom
if (curr[0] + 1) <=(len(grid) -1) and grid[curr[0]+1][curr[1]] == 1:
grid[curr[0]+1][curr[1]]=2
que.put([curr[0] + 1, curr[1], level+1])
# right
if (curr[1] + 1) <= (len(grid[0])-1) and grid[curr[0]][curr[1]+1] == 1:
grid[curr[0]][curr[1]+1]=2
que.put([curr[0], curr[1] + 1, level+1])
# left
if (curr[1] - 1) >= 0 and grid[curr[0]][curr[1] - 1] == 1:
grid[curr[0]][curr[1] - 1]=2
que.put([curr[0], curr[1] - 1, level+1])
if level > ans:
ans=level
# print(ans)
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
return -1
return ans