Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab2ef8b

Browse files
committed
2020-03-03
1 parent 022ef83 commit ab2ef8b

File tree

2 files changed

+38
-24
lines changed

2 files changed

+38
-24
lines changed

0994.腐烂的橘子/0994-腐烂的橘子.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,40 @@ def orangesRotting(self, grid):
44
:type grid: List[List[int]]
55
:rtype: int
66
"""
7+
from collections import deque
8+
if not grid or not grid[0]:
9+
return 0
10+
11+
m, n = len(grid), len(grid[0])
712
dx = [1, -1, 0, 0]
813
dy = [0, 0, 1, -1]
9-
rotlist = list()
10-
for i in range(len(grid)):
11-
for j in range(len(grid[0])):
14+
15+
queue = deque()
16+
for i in range(m):
17+
for j in range(n):
1218
if grid[i][j] == 2:
13-
rotlist.append([i, j])
14-
minute = 0
15-
while(rotlist):
16-
newrotlist = list()
17-
for rotnode in rotlist:
18-
x0 = rotnode[0]
19-
y0 = rotnode[1]
20-
19+
queue.append((i, j))
20+
21+
res = 0
22+
while queue:
23+
for i in range(len(queue)):
24+
pair = queue.popleft()
25+
x0, y0 = pair[0], pair[1]
2126
for k in range(4):
2227
x = x0 + dx[k]
2328
y = y0 + dy[k]
24-
25-
if 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] == 1:
29+
30+
if 0 <= x < m and 0 <= y < n and grid[x][y] == 1:
2631
grid[x][y] = 2
27-
newrotlist.append([x,y])
28-
if not newrotlist:
32+
queue.append((x, y))
33+
if not queue:
2934
break
30-
31-
rotlist = newrotlist[:]
32-
minute += 1
33-
34-
for row in grid:
35-
for i in row:
36-
if i == 1:#»¹ÓÐÐÂÏʵÄ
35+
res += 1
36+
for i in range(m):
37+
for j in range(n):
38+
if grid[i][j] == 1:
3739
return -1
38-
return minute
39-
40+
return res
41+
42+
43+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution(object):
2+
def merge(self, A, m, B, n):
3+
"""
4+
:type A: List[int]
5+
:type m: int
6+
:type B: List[int]
7+
:type n: int
8+
:rtype: None Do not return anything, modify A in-place instead.
9+
"""
10+
A[:] = sorted(A[:m] + B)

0 commit comments

Comments
 (0)