Unit 11 Session 2 Standard (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
1
.0
s) and perform BFS to calculate the distance to the nearest zombie for all human cells (1
s).0
.HAPPY CASE
Input: grid = [
[0,0,0],
[0,1,0],
[0,0,0]
]
Output: [
[0,0,0],
[0,1,0],
[0,0,0]
]
Explanation: The nearest zombie for the human at (1, 1) is at distance 1.
EDGE CASE
Input: grid = [
[0,0,0],
[0,1,0],
[1,1,1]
]
Output: [
[0,0,0],
[0,1,0],
[1,2,1]
]
Explanation: The humans at the bottom are at distances 1 and 2 from the nearest zombies.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Grid Traversal problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Start BFS from all zombie cells (0
s) and calculate the distance for each human cell (1
) to the nearest zombie. Initialize the distances for all zombie cells as 0
and propagate the distance as we traverse the grid.
1) Initialize a 2D array `distances` to store the distance of each cell to the nearest zombie.
2) Add all zombie cells to the BFS queue and mark their distances as `0`.
3) Perform BFS:
a) For each cell, check its valid neighbors.
b) If the neighbor is a human cell, update its distance and continue BFS.
4) Return the `distances` grid.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
from collections import deque
# Helper function to find valid next moves
def next_moves(position, grid, visited):
row, col = position
rows, cols = len(grid), len(grid[0])
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # Up, Down, Left, Right
valid_moves = []
for d_row, d_col in directions:
new_row, new_col = row + d_row, col + d_col
if 0 <= new_row < rows and 0 <= new_col < cols and not visited[new_row][new_col]:
valid_moves.append((new_row, new_col))
return valid_moves
# BFS function to calculate the nearest zombie distances
def nearest_zombie(grid):
m, n = len(grid), len(grid[0])
# Initialize distances with -1, which will be updated during BFS
distances = [[-1] * n for _ in range(m)]
# Initialize the queue and visited set
queue = deque()
visited = [[False] * n for _ in range(m)]
# Start BFS from all zombie cells (grid[row][col] == 0)
for row in range(m):
for col in range(n):
if grid[row][col] == 0: # Zombie
queue.append((row, col))
distances[row][col] = 0 # Distance to the nearest zombie is 0
visited[row][col] = True
# Perform BFS
while queue:
current_row, current_col = queue.popleft()
# Get the valid next moves
for next_row, next_col in next_moves((current_row, current_col), grid, visited):
# Update the distance for the human cell (grid[row][col] == 1)
distances[next_row][next_col] = distances[current_row][current_col] + 1
visited[next_row][next_col] = True
queue.append((next_row, next_col))
return distances
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume m
is the number of rows and n
is the number of columns in the grid.
O(m * n)
because each cell is processed once during BFS.O(m * n)
due to the queue, visited array, and distance array.