3.9 KiB
id, title, difficulty, tags, status, date_solved, leetcode_url, review_needed
| id | title | difficulty | tags | status | date_solved | leetcode_url | review_needed | |||
|---|---|---|---|---|---|---|---|---|---|---|
| 2017 | Grid Game | Medium |
|
Solved | 2026-06-05 | https://leetcode.com/problems/grid-game/ | false |
2017. Grid Game
[!info] Problem Link: LeetCode - Grid Game
📝 Problem Description
You are given a 0-indexed 2D array grid of size 2 x n, where grid[r][c] represents the number of points at cell (r, c). Two robots are playing a game on this grid.
Both robots start at (0, 0) and want to reach (1, n-1). Each robot may only move to the right ((r, c) -> (r, c + 1)) or down ((r, c) -> (r + 1, c)).
- Robot 1 moves first. It collects all points on its path, and those cells are set to
0. - Robot 2 moves second. It collects points from the remaining cells on its path.
- Goal: Robot 1 wants to minimize the points Robot 2 collects. Robot 2 wants to maximize its own points.
Return the number of points collected by the second robot assuming both play optimally.
📥 Example 1
Input:
grid = [[2,5,4],[1,5,1]]Output:4Explanation: Robot 1 takes path (0,0) -> (0,1) -> (1,1) -> (1,2). The cells (0,0), (0,1), (1,1), and (1,2) become 0. Robot 2 can then take path (0,0) -> (0,1) -> (0,2) -> (1,2) to collect 4 points (only grid[0][2] remains).
📥 Example 2
Input:
grid = [[3,3,1],[8,5,2]]Output:4
💡 Approaches & Explanations
Approach 1: Prefix Sums — Optimal
Since there are only 2 rows, each robot must transition from the top row to the bottom row exactly once. If Robot 1 "drops" to the second row at column i, then:
- The only points left in the top row are from column
i + 1ton - 1. - The only points left in the bottom row are from column
0toi - 1.
Robot 2 will optimally choose the maximum of these two remaining segments. Robot 1, knowing this, will choose the "drop" column i that minimizes Robot 2's maximum possible score.
- Calculate the total sum of the top row.
- Iterate through each column
i(representing Robot 1's drop point):- Keep track of the points remaining in the top row (sum of
grid[0][i+1:]). - Keep track of the points remaining in the bottom row (sum of
grid[1][:i]). - For each
i, Robot 2's score ismax(top_remaining, bottom_remaining). - Minimize this score across all
i.
- Keep track of the points remaining in the top row (sum of
📊 Complexity Analysis
- Time Complexity:
O(N)whereNis the number of columns. We traverse the grid twice (once for the total sum and once to find the optimal column). - Space Complexity:
O(1)if we calculate sums on the fly (ignoring input space).
💻 Code Implementations
Python3
class Solution:
def gridGame(self, grid: List[List[int]]) -> int:
n = len(grid[0])
top_sum = sum(grid[0])
bottom_sum = 0
res = float("inf")
for i in range(n):
# If Robot 1 drops at column i:
# Robot 2 can either take the remaining top part...
top_sum -= grid[0][i]
# ...or the remaining bottom part.
# (bottom_sum here represents grid[1][0...i-1])
robot2_score = max(top_sum, bottom_sum)
res = min(res, robot2_score)
# Prepare bottom_sum for the next iteration (i + 1)
bottom_sum += grid[1][i]
return res
🧠 Key Takeaways & Lessons
- Game Theory Simplification: In problems where Robot 1 wants to minimize Robot 2's maximum, look for the bottleneck. Here, the bottleneck is Robot 1's single vertical move.
- Prefix/Suffix Sum Strategy: When dealing with split ranges (e.g., everything before
iand everything afteri), prefix and suffix sums are the most efficient way to compute segment totals inO(1).