Robot 1 must go right along row 0, drop down to row 1 at some column, then continue right. After Robot 1 collects its points (setting those cells to 0), Robot 2 follows optimally. Robot 1 wants to minimize Robot 2's maximum possible score. We try every possible column where Robot 1 drops down and simulate Robot 2's best response.
i where Robot 1 drops down:i, then the bottom row from column i onward.i, bottom row before i).i.class Solution:
def gridGame(self, grid: List[List[int]]) -> int:
cols = len(grid[0])
res = float('inf')
top1 = 0
for i in range(cols):
top1 += grid[0][i]
bottom1 = 0
for j in range(i, cols):
bottom1 += grid[1][j]
top2 = robot2 = 0
for j in range(cols):
if j > i:
top2 += grid[0][j]
bottom2 = 0
for k in range(j, i):
bottom2 += grid[1][k]
robot2 = max(robot2, top2 + bottom2)
res = min(res, robot2)
return resAfter Robot 1 drops at column i, Robot 2 can only collect from two disjoint regions: the top row after column i, or the bottom row before column i. Robot 2 will choose whichever region has more points. Using prefix sums, we can compute these region totals in O(1) per column.
i where Robot 1 drops:top = preRow1[N-1] - preRow1[i] (sum of top row after column i).bottom = preRow2[i-1] if i > 0, else 0 (sum of bottom row before column i).max(top, bottom).i.class Solution:
def gridGame(self, grid: List[List[int]]) -> int:
N = len(grid[0])
preRow1, preRow2 = grid[0].copy(), grid[1].copy()
for i in range(1, N):
preRow1[i] += preRow1[i - 1]
preRow2[i] += preRow2[i - 1]
res = float("inf")
for i in range(N):
top = preRow1[-1] - preRow1[i]
bottom = preRow2[i - 1] if i > 0 else 0
secondRobot = max(top, bottom)
res = min(res, secondRobot)
return resWe can avoid storing prefix arrays by maintaining running sums. Start with topSum as the total of the top row and bottomSum as 0. As we iterate, subtract from topSum (simulating Robot 1 taking cells from the top) and add to bottomSum (accumulating what Robot 2 could take from below).
topSum as the sum of the entire top row, and bottomSum as 0.i:grid[0][i] from topSum (Robot 1 takes this cell).max(topSum, bottomSum).grid[1][i] to bottomSum (this cell is now unavailable to Robot 2).class Solution:
def gridGame(self, grid: List[List[int]]) -> int:
res = float("inf")
topSum = sum(grid[0])
bottomSum = 0
for i in range(len(grid[0])):
topSum -= grid[0][i]
res = min(res, max(topSum, bottomSum))
bottomSum += grid[1][i]
return resA common misunderstanding is thinking Robot 1 should maximize its own score. The problem asks to minimize the score of Robot 2, which is a different objective. Robot 1 must strategically choose where to drop down to leave the least valuable remaining cells for Robot 2, not just greedily collect the most points.
After Robot 1 drops at column i, Robot 2 can only collect from either the top row after column i OR the bottom row before column i, never both. These regions are mutually exclusive because Robot 2 cannot move left. Failing to recognize this leads to overcounting what Robot 2 can collect.
The grid values can be up to 10^5 and the grid can have up to 5 * 10^4 columns. The sum of all values can exceed the 32-bit integer limit. Use long or 64-bit integers for the prefix sums and running totals to avoid overflow errors that produce incorrect results.