-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotting Oranges.py
54 lines (34 loc) · 1.49 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
51
52
53
54
# Time Complexity: 𝑂 ( m * n )
# Space Complexity: 𝑂 ( m * n )
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
from collections import deque
queue = deque()
rotten_time = 0
visited = set()
for i in range(len(grid)):
for j in range(len(grid[i])):
if (i,j) not in visited and grid[i][j] == 1:
visited.add((i,j))
elif grid[i][j] == 2:
queue.append((i,j,rotten_time))
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 2:
rotten_time = self.checkRotting(queue,grid,i,j,visited,rotten_time,len(grid),len(grid[i]))
return -1 if visited else rotten_time
def checkRotting(self,queue,grid,row,col,visited,rotten_time,i,j):
while queue:
row,col,rotten_time = queue.popleft()
for (x,y) in [(1,0),(0,1),(-1,0),(0,-1)]:
curr_x = row + x
curr_y = col + y
if self.isSafe(curr_x,curr_y,i,j) and grid[curr_x][curr_y] == 1:
grid[curr_x][curr_y] = 2
visited.remove((curr_x,curr_y))
queue.append((curr_x,curr_y,rotten_time+1))
return rotten_time
def isSafe(self,x,y,i,j):
if x >= 0 and x < i and y >= 0 and y < j:
return True
return False