2017. Grid Game
UnknownView on LeetCode
Time: O(n)
Space: O(1)
Problem Overview
Grid Game (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Matrix pattern in coding interviews. First robot splits row0 (left prefix) and row1 (right prefix); second robot takes max of remainders.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
First robot splits row0 (left prefix) and row1 (right prefix); second robot takes max of remainders.
Related patterns: Array, Matrix, Simulation
2017.cs
C#
// Approach: First robot splits row0 (left prefix) and row1 (right prefix); second robot takes max of remainders.
// Time: O(n) Space: O(1)
public class Solution
{
public long GridGame(int[][] grid)
{
int n = grid[0].Length;
long ans = long.MaxValue;
long sumRow0 = 0;
for (int i = 0; i < n; i++)
sumRow0 += grid[0][i];
long sumRow1 = 0;
for (int i = 0; i < n; ++i)
{
sumRow0 -= grid[0][i];
ans = Math.Min(ans, Math.Max(sumRow0, sumRow1));
sumRow1 += grid[1][i];
}
return ans;
}
}Was this solution helpful?
Related Problems
- 498. Diagonal Traverse(Medium)
- 885. Spiral Matrix III(Medium)
- 1260. Shift 2D Grid(Easy)
- 1861. Rotating the Box(Easy)
- 2022. Convert 1D Array Into 2D Array(Medium)
- 2257. Count Unguarded Cells in the Grid(Unknown)