Solving Leetcode Interviews in Seconds with AI: Escape the Spreading Fire
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2258" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
You are given a 0-indexed 2D integer array grid of size m x n which represents a field. Each cell has one of three values: 0 represents grass, 1 represents fire, 2 represents a wall that you and fire cannot pass through. You are situated in the top-left cell, (0, 0), and you want to travel to the safehouse at the bottom-right cell, (m - 1, n - 1). Every minute, you may move to an adjacent grass cell. After your move, every fire cell will spread to all adjacent cells that are not walls. Return the maximum number of minutes that you can stay in your initial position before moving while still safely reaching the safehouse. If this is impossible, return -1. If you can always reach the safehouse regardless of the minutes stayed, return 109. Note that even if the fire spreads to the safehouse immediately after you have reached it, it will be counted as safely reaching the safehouse. A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching). Example 1: Input: grid = [[0,2,0,0,0,0,0],[0,0,0,2,2,1,0],[0,2,0,0,1,2,0],[0,0,2,2,2,0,2],[0,0,0,0,0,0,0]] Output: 3 Explanation: The figure above shows the scenario where you stay in the initial position for 3 minutes. You will still be able to safely reach the safehouse. Staying for more than 3 minutes will not allow you to safely reach the safehouse. Example 2: Input: grid = [[0,0,0,0],[0,1,2,0],[0,2,0,0]] Output: -1 Explanation: The figure above shows the scenario where you immediately move towards the safehouse. Fire will spread to any cell you move towards and it is impossible to safely reach the safehouse. Thus, -1 is returned. Example 3: Input: grid = [[0,0,0],[2,2,0],[1,2,0]] Output: 1000000000 Explanation: The figure above shows the initial grid. Notice that the fire is contained by walls and you will always be able to safely reach the safehouse. Thus, 109 is returned. Constraints: m == grid.length n == grid[i].length 2 <= m, n <= 300 4 <= m n <= 2 104 grid[i][j] is either 0, 1, or 2. grid[0][0] == grid[m - 1][n - 1] == 0
Explanation
Here's the breakdown of the solution:
- Binary Search: Perform a binary search on the possible waiting times (from 0 to a large number like 109) to find the maximum waiting time for which it's still possible to reach the safehouse.
- Simulate Fire Spread: For each waiting time considered in the binary search, simulate the fire spreading on the grid. Use a breadth-first search (BFS) to determine how long it takes for the fire to reach each cell.
Check Reachability: After simulating the fire, use another BFS to determine if you can reach the safehouse before the fire does, given the specified waiting time.
Runtime Complexity: O(m n log(109)), where m and n are the dimensions of the grid. Storage Complexity: O(m * n)
Code
from collections import deque
def maximumMinutes(grid):
m, n = len(grid), len(grid[0])
INF = 10**9
def bfs(start_grid, start_time):
fire_grid = [[INF] * n for _ in range(m)]
q = deque()
for r in range(m):
for c in range(n):
if start_grid[r][c] == 1:
fire_grid[r][c] = 0
q.append((r, c))
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
time = 0
while q:
size = len(q)
for _ in range(size):
r, c = q.popleft()
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < m and 0 <= nc < n and start_grid[nr][nc] != 2 and fire_grid[nr][nc] == INF:
fire_grid[nr][nc] = time + 1
q.append((nr, nc))
time += 1
return fire_grid
def can_reach(wait_time, fire_grid):
player_grid = [[INF] * n for _ in range(m)]
player_grid[0][0] = wait_time
q = deque([(0, 0)])
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while q:
r, c = q.popleft()
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < m and 0 <= nc < n and grid[nr][nc] != 2 and player_grid[nr][nc] == INF and player_grid[r][c] + 1 < fire_grid[nr][nc]:
player_grid[nr][nc] = player_grid[r][c] + 1
q.append((nr, nc))
return player_grid[m - 1][n - 1] != INF
fire_distances = bfs(grid, 0)
if fire_distances[0][0] == 0:
return -1
low = 0
high = INF
ans = -1
while low <= high:
mid = (low + high) // 2
if can_reach(mid, fire_distances):
ans = mid
low = mid + 1
else:
high = mid - 1
return ans