Grid Game - Problem
Grid Game is a fascinating competitive strategy problem where two robots navigate a
๐ฎ 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
๐ Movement Rules:
๐ง 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.
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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code