-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathRotting Oranges.py
50 lines (48 loc) · 2 KB
/
Rotting Oranges.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
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
visited = set()
res = 0
def bfs(grid):
que = collections.deque()
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 2:
que.append((i,j))
que.append(None)
count = 0
while len(que)>0:
left , right = 0,0
# This other while loop is to make sure that we traverse from all the rotten oranges in one turn.
while que[0] != None:
r,c = que.popleft()
for Cols in [-1,1]:
if c+Cols>=0 and c+Cols<len(grid[0]) and grid[r][c+Cols]==1:
grid[r][c+Cols] = 2
left += 1
que.append((r,c+Cols))
for Rows in [-1,1]:
if r+Rows>=0 and r+Rows<len(grid) and grid[r+Rows][c]==1:
grid[r+Rows][c] = 2
right += 1
que.append((r+Rows,c))
'''
if either left or right or both is incremented it means that we have moved in either direction,
and then this will be counted as a turn(or minute as per the problem description.).
'''
if left is not 0 or right is not 0:
count+=1
que.popleft()
if len(que)>0:
'''
This is required to terminate the loop and prevent infinite loop.
This only appends a None if there is still some values in the que,
else we have completed our traversal and we can stop going any futher.
'''
que.append(None)
return count
res = bfs(grid)
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
return -1
return res