-
-
Notifications
You must be signed in to change notification settings - Fork 422
/
994_Rotting_Oranges.py
47 lines (37 loc) · 1.23 KB
/
994_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
# Time Complexity = O(rows*columns)
# Space Complexity = O(rows*columns)
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
n = len(grid)
m = len(grid[0])
count = 0
rotten = []
for i in range(n):
for j in range(m):
if grid[i][j]==2:
rotten.append((i,j))
count+=1
elif grid[i][j] == 1:
count+=1
if count == 0:
return 0
cycles = 0
while rotten:
count-=len(rotten)
newRotten = []
for i,j in rotten:
self.destroy(grid,i+1,j,n,m,newRotten)
self.destroy(grid,i-1,j,n,m,newRotten)
self.destroy(grid,i,j+1,n,m,newRotten)
self.destroy(grid,i,j-1,n,m,newRotten)
rotten = newRotten
cycles+=1
if count>0:
return -1
else:
return cycles-1
def destroy(self, grid, i, j, n, m, rotten):
if 0<=i<n and 0<=j<m:
if grid[i][j] == 1:
rotten.append((i,j))
grid[i][j]=2