Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def orangesRotting(self, grid: List[List[int]]) -> int:
- rows, cols = len(grid), len(grid[0])
- q = collections.deque()
- time = 0
- total_fresh = 0
- count_fresh = 0
- for r in range(rows):
- for c in range(cols):
- if grid[r][c] == 1:
- total_fresh += 1
- if grid[r][c] == 2:
- q.append((r, c))
- directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
- while(q) and total_fresh!=count_fresh:
- size = len(q)
- for _ in range(size):
- r, c = q.popleft()
- for dr, dc in directions:
- row = r + dr
- col = c + dc
- if row in range(rows) and col in range(cols) and grid[row][col] == 1:
- grid[row][col] = 2
- q.append((row, col))
- count_fresh += 1
- time = time + 1
- return -1 if total_fresh!=count_fresh else time
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement