Unit 11 Session 1 Standard (Click for link to problem statements)
Unit 11 Session 1 Advanced (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?
0
if no troops exist.0
and cannot be passed through.HAPPY CASE
Input: battlefield = [
[0,2,1,0],
[4,0,0,3],
[1,0,0,4],
[0,3,2,0]
]
Output: 7
Explanation: You can start at (1,3), capture 3 troops, move to (2,3), and capture 4 troops, for a total of 7 troops.
EDGE CASE
Input: battlefield = [
[0,0],
[0,0]
]
Output: 0
Explanation: There are no troops on the battlefield, so the output is 0.
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: Use BFS to explore each connected component of troops on the battlefield. Starting from each unvisited troop cell, perform BFS to collect all troops in the connected component and update the maximum number of troops captured.
1) Define a helper function `next_moves` to get valid neighboring troop cells (up, down, left, right).
2) Define a BFS function to explore a connected component of troops starting from a given cell.
3) Initialize a `visited` set to keep track of visited cells.
4) Iterate through the battlefield grid:
a) If a cell contains troops and has not been visited, perform BFS to find the total number of troops in the connected component.
b) Update the maximum number of troops captured.
5) Return the maximum number of troops captured.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
from collections import deque
# Helper function to get valid neighboring troop cells
def next_moves(battlefield, row, column):
moves = [
(row + 1, column), # down
(row - 1, column), # up
(row, column + 1), # right
(row, column - 1) # left
]
possible = []
for r, c in moves:
if 0 <= r < len(battlefield) and 0 <= c < len(battlefield[0]) and battlefield[r][c] > 0:
possible.append((r, c))
return possible
# BFS function to explore a connected component of troops
def bfs(battlefield, row, column, visited):
queue = deque([(row, column)])
visited.add((row, column))
total_troops = battlefield[row][column]
while queue:
r, c = queue.popleft()
for nr, nc in next_moves(battlefield, r, c):
if (nr, nc) not in visited:
visited.add((nr, nc))
total_troops += battlefield[nr][nc]
queue.append((nr, nc))
return total_troops
# Main function to find the maximum number of troops that can be captured
def capture_max_troops(battlefield):
if not battlefield or not battlefield[0]:
return 0
max_troops = 0
visited = set()
for row in range(len(battlefield)):
for col in range(len(battlefield[0])):
if battlefield[row][col] > 0 and (row, col) not in visited:
# Perform BFS/DFS to find total troops in this connected component
max_troops = max(max_troops, bfs(battlefield, row, col, visited))
return max_troops
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Example 1:
next_moves
.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 visited at most once during BFS.O(m * n)
due to the queue storing positions of cells and the visited set.