-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path994.rotting-oranges.py
More file actions
63 lines (47 loc) · 1.83 KB
/
Copy path994.rotting-oranges.py
File metadata and controls
63 lines (47 loc) · 1.83 KB
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
55
56
57
58
59
60
61
62
63
class Solution:
def orangesRotting(self, grid: List[List[int]]) -> int:
minute = 0
rows = len(grid)
columns = len(grid[0])
def check_1(x, y):
return True if grid[x][y] == 1 else False
def rot(x,y, bool):
if x-1 >= 0:
if check_1(x-1, y):
grid[x-1][y] = 2
bool = True
if x+1 < rows:
if check_1(x+1, y):
grid[x+1][y] = 2
bool = True
if y-1 >= 0:
if check_1(x, y-1):
grid[x][y-1] = 2
bool = True
if y+1 < columns:
if check_1(x, y+1):
grid[x][y+1] = 2
bool = True
return bool
for _ in range(100):
coordinates = []
one = []
flag = False
for x in range(rows):
for y in range(columns):
if grid[x][y] == 2:
grid[x][y] = 3
coordinates.append([x,y])
if grid[x][y] == 1:
one.append([x,y])
if len(coordinates) == 0 and len(one) != 0:
return -1
if len(coordinates) == 0:
break
for i in range(len(coordinates)):
flag = rot(coordinates[i][0], coordinates[i][1], flag)
# print(flag)
if flag:
minute += 1
# print(grid, minute)
return minute