Grid Game - Problem
Grid Game is a fascinating competitive strategy problem where two robots navigate a 2 ร— n grid to collect points.

๐ŸŽฎ The Setup: You have a grid with 2 rows and n columns, where each cell contains points. Two robots start at the top-left corner (0, 0) and must reach the bottom-right corner (1, n-1).

๐Ÿ“‹ Movement Rules:
  • Robots can only move right or down
  • The first robot moves first, collecting all points on its path
  • After the first robot finishes, all visited cells become 0
  • The second robot then takes its turn on the modified grid

๐Ÿง  Strategic Twist: The first robot wants to minimize the second robot's score, while the second robot wants to maximize its own score. Both play optimally!

Goal: Determine how many points the second robot will collect when both robots play their best strategy.

Input & Output

example_1.py โ€” Basic Grid
$ Input: grid = [[2,5,4],[1,2,3]]
โ€บ Output: 4
๐Ÿ’ก Note: The first robot can turn down at column 1, leaving top region [4] and bottom region [1]. Second robot gets max(4,1) = 4. This is actually not optimal - turning at column 2 gives max(0,3) = 3.
example_2.py โ€” Smaller Grid
$ Input: grid = [[3,3,1],[8,5,2]]
โ€บ Output: 4
๐Ÿ’ก Note: Turn options: Column 0โ†’max(4,0)=4, Column 1โ†’max(1,8)=8, Column 2โ†’max(0,13)=13. Minimum is 4.
example_3.py โ€” Edge Case
$ Input: grid = [[1,1,1,1],[2,2,2,2]]
โ€บ Output: 2
๐Ÿ’ก Note: All turn columns give the same result due to uniform distribution. Turn at any middle column gives balanced regions.

Constraints

  • grid.length == 2
  • n == grid[r].length
  • 1 โ‰ค n โ‰ค 5 ร— 104
  • 1 โ‰ค grid[r][c] โ‰ค 105
  • Grid has exactly 2 rows

Visualization

Tap to expand
Grid Game: Strategic AnalysisR1First Robot(Minimizer)R2Second Robot(Maximizer)Game TheoryRobot 1: min(opponent score)Robot 2: max(own score)Minimax Problem!Key InsightPath = Choice of turn columnCreates exactly 2 regionsO(n) solution!Optimal Strategy VisualizationABCDEFGHIJRegion 1: C+D+ERegion 2: FRobot 2 picks max(Region1, Region2)๐ŸŽฏ **Key Insight:** Robot 1 minimizes max(top_suffix, bottom_prefix) across all turn columns
Understanding the Visualization
1
Identify the Pattern
Every path creates exactly two uncollected regions
2
Mathematical Insight
The regions are always top-right and bottom-left of the turn point
3
Optimization
Second robot picks the better region, first robot minimizes this choice
Key Takeaway
๐ŸŽฏ Key Insight: The problem reduces to finding the optimal turn column that minimizes the maximum of two complementary regions, transforming a complex path problem into an elegant O(n) optimization.
Asked in
Google 42 Amazon 38 Meta 25 Microsoft 18
32.8K Views
Medium-High Frequency
~18 min Avg. Time
1.5K 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