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
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)
- 27. Remove Element(Easy)