Grid Game - Problem

You are given a 0-indexed 2D array grid of size 2 x n, where grid[r][c] represents the number of points at position (r, c) on the matrix.

Two robots are playing a game on this matrix. Both robots initially start at (0, 0) and want to reach (1, n-1). Each robot may only move right ((r, c) to (r, c + 1)) or down ((r, c) to (r + 1, c)).

At the start of the game, the first robot moves from (0, 0) to (1, n-1), collecting all the points from the cells on its path. For all cells (r, c) traversed on the path, grid[r][c] is set to 0. Then, the second robot moves from (0, 0) to (1, n-1), collecting the points on its path.

The first robot wants to minimize the number of points collected by the second robot. In contrast, the second robot wants to maximize the number of points it collects. If both robots play optimally, return the number of points collected by the second robot.

Input & Output

Example 1 — Basic Grid
$ Input: grid = [[2,5,4],[8,10,3]]
Output: 4
💡 Note: The first robot goes down at position 1, taking path (0,0)→(0,1)→(1,1)→(1,2), collecting 2+5+10+3=20 points. The second robot chooses the top path to collect 0+0+4=4 points.
Example 2 — Better Bottom Path
$ Input: grid = [[1,3,1,15],[1,3,3,1]]
Output: 7
💡 Note: The first robot goes down at position 2, collecting 1+3+1+3+1=9 points. The remaining points allow the second robot to collect max(0+0+15, 1+3+0) = max(15,4) = 15. But the optimal first robot strategy gives second robot only 7.
Example 3 — Equal Rows
$ Input: grid = [[1,1,1],[1,1,1]]
Output: 1
💡 Note: With identical rows, the first robot can force the second robot to get at most 1 point regardless of strategy.

Constraints

  • grid.length == 2
  • n == grid[r].length
  • 1 ≤ n ≤ 5 × 104
  • 1 ≤ grid[r][c] ≤ 105

Visualization

Tap to expand
Grid Game: Two Robots Competing for PointsInitial Grid2548103After First Robot004000Second Robot Choice004000Start: (0,0)First robot pathSecond robot optimalFirst robot: Collected 2+5+10+3 = 20 pointsSecond robot: Takes top path for 4 pointsOutput: 4 (second robot score)🎯 First robot minimizes, second robot maximizes with optimal play
Understanding the Visualization
1
Initial Grid
2×n grid with point values, both robots start at (0,0)
2
First Robot Move
First robot chooses path to minimize second robot's score
3
Second Robot Move
Second robot takes optimal path on remaining points
Key Takeaway
🎯 Key Insight: The first robot must choose a path that minimizes the second robot's maximum possible score
Asked in
Google 15 Amazon 12 Microsoft 8
32.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen