[
bfs
]
BinarySearch 0255 Wildfire
Problem statement
https://binarysearch.com/problems/Wildfire/
Solution
Equal to leetcode 0994. Rotting Oranges.
Complexity
It is O(mn)
for time and space.
Code
class Solution:
def solve(self, grid):
if not grid: return 0
m, n, queue, fresh = len(grid), len(grid[0]), deque(), 0
for i,j in product(range(m), range(n)):
if grid[i][j] == 2: queue.append((i,j))
if grid[i][j] == 1: fresh += 1
dirs = [[1,0],[-1,0],[0,1],[0,-1]]
levels = 0
while queue:
levels += 1
for _ in range(len(queue)):
x, y = queue.popleft()
for dx, dy in dirs:
if 0<=x+dx<m and 0<=y+dy<n and grid[x+dx][y+dy] == 1:
fresh -= 1
grid[x+dx][y+dy] = 2
queue.append((x+dx, y+dy))
return -1 if fresh != 0 else max(levels-1, 0)