-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathceleb.py
45 lines (37 loc) · 1.08 KB
/
celeb.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
def orangesRotting(elemnts):
if not elemnts or len(elemnts) == 0:
return 0
n = len(elemnts)
m = len(elemnts[0])
rotten = []
for i in range(n):
for j in range(m):
if elemnts[i][j] == 2:
rotten.append((i, j))
mins = 0
def dfs(rotten):
count = []
for i, j in rotten:
if i > 0 and rotten[i - 1][j] == 1:
count.append((i - 1, j))
elemnts[i - 1][j] = 2
if j > 0 and rotten[i][j - 1] == 1:
count.append((i, j - 1))
elemnts[i][j - 1] = 2
if i < n - 1 and rotten[i][j] == 1:
count.append((i, j))
elemnts[i][j] = 2
if j < m - 1 and rotten[i][j] == 1:
count.append((i, j))
elemnts[i][j] = 2
return count
while rotten:
rotten = dfs(rotten)
if not rotten:
break
mins += 1
for i in range(n):
for j in range(m):
if elemnts[i][j] == 1:
return -1
return mins