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: 8
💡 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 and removing those cells. The second robot then has two remaining path options: top path with 0+0+4=4 points or bottom path with 8+0+0=8 points. The second robot chooses the bottom path to get 8 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 - Optimal Solution INPUT 2 x n Grid (n=3) 2 5 4 Row 0 8 10 3 Row 1 S E grid = [ [2, 5, 4], [8, 10, 3] ] Robot 1: Minimizes R2 score Robot 2: Maximizes own score Move: Right or Down only Path: (0,0) to (1,n-1) ALGORITHM STEPS 1 Calculate Prefix Sums Top right: 5+4=9, 4, 0 Bottom left: 0, 8, 8+10=18 2 Try Each Turn Point R1 goes down at col i R2 picks: top-right OR bottom-left 3 Evaluate Turn Points i=0: max(9, 0)=9 i=1: max(4, 8)=8 i=2: max(0, 18)=18 4 Find Minimum R1 picks i=1 (turn at col 1) R2 best = max(4, 8) = 8 Wait! Check i=1 again... Optimal: Turn at col 1 R2 chooses top-right: [4] min(9, 8, 18) = actual check Result: 4 FINAL RESULT Robot 1 Optimal Path (Green) 2 5 4 8 10 3 R1 Path (set to 0) R2 Collects (4 pts) Untouched OUTPUT 4 R1 turns at column 1 R2 takes top-right: [4] Optimal play = 4 points Key Insight: Robot 1 must turn down exactly once. At turn column i, Robot 2 gets either the sum of top-right cells (grid[0][i+1...n-1]) OR bottom-left cells (grid[1][0...i-1]). Robot 2 picks the maximum of these two. Robot 1 minimizes this by choosing optimal turn point. Use prefix sums for O(n) solution. TutorialsPoint - Grid Game | Optimal Solution Approach
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