The Grid Game coding problem features a grid with points in each cell. Two robots play a game. Robot 1 starts at (0,0) and moves to (1, N-1) (only moving right or down). It collects all points on its path, setting those cells to 0. Robot 2 then makes the exact same journey, collecting the remaining points. Robot 1 wants to minimize Robot 2's score, while Robot 2 wants to maximize its own score. You need to return the score Robot 2 will achieve assuming both play optimally.
Microsoft and Amazon use this Prefix Sum and Array pattern problem to test your ability to model adversarial games. Because the grid only has 2 rows, Robot 1 can only move down exactly once. This drastically reduces the search space. It evaluates if you can identify that Robot 1's choice splits the remaining points for Robot 2 into two distinct, non-overlapping segments.
This problem relies on Prefix Sums and Min-Max Logic.
i, it leaves two segments of points for Robot 2:
i + 1 to N - 1.0 to i - 1.i is max(top_remaining, bottom_preceding).i that minimizes this maximum score.Grid:
[2, 5, 4]
[1, 5, 1]
max([5, 4], []) = max(9, 0) = 9.max([4], [1]) = max(4, 1) = 4.max([], [1, 5]) = max(0, 6) = 6.
Robot 1 wants to minimize R2's score, so R1 chooses column 1.
Robot 2's score is 4.In 2-row grid pathfinding problems, the key insight is always that a path can only move "down" exactly once. This turns the problem into a 1D array search where you just need to pick the optimal "drop point."
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count Submatrices With Equal Frequency of X and Y | Medium | Solve | |
| Increment Submatrices by One | Medium | Solve | |
| Matrix Block Sum | Medium | Solve | |
| Count Submatrices with Top-Left Element and Sum Less Than k | Medium | Solve | |
| Largest Magic Square | Medium | Solve |