Solving Leetcode Interviews in Seconds with AI: Find a Safe Walk Through a Grid
Introduction
In this blog post, we will explore how to solve the LeetCode problem "3286" 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 an m x n binary matrix grid and an integer health. You start on the upper-left corner (0, 0) and would like to get to the lower-right corner (m - 1, n - 1). You can move up, down, left, or right from one cell to another adjacent cell as long as your health remains positive. Cells (i, j) with grid[i][j] = 1 are considered unsafe and reduce your health by 1. Return true if you can reach the final cell with a health value of 1 or more, and false otherwise. Example 1: Input: grid = [[0,1,0,0,0],[0,1,0,1,0],[0,0,0,1,0]], health = 1 Output: true Explanation: The final cell can be reached safely by walking along the gray cells below. Example 2: Input: grid = [[0,1,1,0,0,0],[1,0,1,0,0,0],[0,1,1,1,0,1],[0,0,1,0,1,0]], health = 3 Output: false Explanation: A minimum of 4 health points is needed to reach the final cell safely. Example 3: Input: grid = [[1,1,1],[1,0,1],[1,1,1]], health = 5 Output: true Explanation: The final cell can be reached safely by walking along the gray cells below. Any path that does not go through the cell (1, 1) is unsafe since your health will drop to 0 when reaching the final cell. Constraints: m == grid.length n == grid[i].length 1 <= m, n <= 50 2 <= m * n 1 <= health <= m + n grid[i][j] is either 0 or 1.
Explanation
- Binary Search for Minimum Initial Health: Perform a binary search on the possible range of initial health values to find the minimum health required to reach the destination.
DFS with Health Tracking: For each health value in the binary search, use Depth-First Search (DFS) to explore the grid. Keep track of the current health as you traverse the grid, decrementing it when encountering unsafe cells (grid[i][j] == 1).
Check Reachability: If the DFS reaches the destination (bottom-right corner) with health >= 1, it means the current health value is sufficient.
Runtime Complexity: O(m*n*log(m+n)), Storage Complexity: O(m*n)
Code
def can_reach_destination(grid, health):
m, n = len(grid), len(grid[0])
def dfs(row, col, current_health, visited):
if row == m - 1 and col == n - 1:
return current_health >= 1
visited[row][col] = True
# Possible moves (up, down, left, right)
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for dr, dc in directions:
new_row, new_col = row + dr, col + dc
if 0 <= new_row < m and 0 <= new_col < n and not visited[new_row][new_col]:
new_health = current_health - grid[new_row][new_col]
if new_health > 0:
if dfs(new_row, new_col, new_health, visited):
return True
return False
visited = [[False] * n for _ in range(m)]
initial_health = health - grid[0][0]
if initial_health <=0:
return False
return dfs(0, 0, initial_health, visited)