Grid Game
Two robots cross a two-row grid of points: choose the first robot's path so the second collects as little as possible.
You are given a grid of points with two rows and ngridgrid[r][c]rc
The first robot makes its whole trip first. It collects the points on every cell of its path, and those cells drop to 0. Then the second robot makes its trip and collects what is left on its path. The first robot wants the second to collect as few points as possible, and the second wants as many as possible. Return the points the second robot collects when both play their best.
Example 1
Output: 4
Example 2
Output: 4
Example 3
Output: 7
The Idea
Picture two people crossing a floor of tiles, one after the other, with coins lying on the tiles. The first person picks up every coin they step on, and the second gets only what is left. The twist here is that the first person does not care about their own coins. They only want to leave the second person as little as possible.
Take Example 1. The top row holds 2, 5 and 4, and the bottom row holds 1, 5 and 1. A robot starts on the 2 in the top-left corner and must end on the 1 in the bottom-right corner. With only right and down moves and only two rows, it goes down exactly once. So a whole path is settled by one choice: the column where the robot goes down. A grid with nn
Look at the picture below, where the columns are numbered from 0. On the left, the first robot goes down in column 1. It collects 2 + 5 + 5 + 1 = 13, and those four cells become 0. On the right is the grid the second robot sees. Only two cells still have points: the 4 in the top right and the 1 in the bottom left.
Which of the two can the second robot get? To reach the 4, it must stay on the top row past column 1, so it can go down only in the last column, and by then the 1 in column 0 is behind it. To reach the 1, it must go down in column 0, and then the top row is out of reach. So it gets the top right or the bottom left, never both, and it takes the bigger one: 4.
Was column 1 the best choice for the first robot? Going down in column 0 leaves the 5 and the 4 on the top row, so the second robot gets 9. Going down in column 2 leaves the 1 and the 5 on the bottom row, so it gets 6. Column 1 leaves only 4, the smallest of the three, and 4 is the answer.
The same holds for any grid. If the first robot goes down in column dd
Solution Approach
Turning that idea into code means settling three things: what the first robot aims for, how to get the two sums for every column quickly, and which type can hold the sums.
What the first robot aims for
Why not let the first robot simply collect as many points as it can? Fewer points left sounds like fewer points for the second robot, but where the leftovers lie matters too. Take the grid [[1, 5, 6], [6, 6, 1]]
Going down in column 1 collects only 13, but leaves a 6 in the top right and a 6 in the bottom left. The second robot can take just one of them, so it gets 6. The first robot therefore looks at what it leaves behind, not at what it takes.
Summing the leftovers for each column
How do we get the top right and the bottom left for every column? The simple way adds up both parts again for each column. That is up to nn
Neighbouring columns share almost all of their sums, though. When the first robot goes down one column further right, the top right loses one cell and the bottom left gains one. So two running totals are enough: topRightbottomLeft
For each column, first subtract that column's top cell from topRightbottomLeftbottomLefttopRight
Holding the sums without overflow
Can the sums get too big? A Java intintlong
Java Solution
The method gridGame
- First, add up the whole top row into
, atopRight . Startlong at 0, andbottomLeft , the smallest answer so far, atbest , so the first column's result always replaces it.Long.MAX_VALUE - Then loop over the columns with
, the column where the first robot goes down. Subtractdown fromgrid[0][down] , so it holds only the top cells to the right oftopRight .down - The second robot takes the larger total,
, saved inMath.max(topRight, bottomLeft) . Keep the smaller ofsecond andbest insecond .best - Add
togrid[1][down] , so it is ready for the next column.bottomLeft - When the loop is done, return
.best
The main[[1, 5, 6], [6, 6, 1]]
import java.util.Arrays;
public class GridGame {
public static long gridGame(int[][] grid) {
int n = grid[0].length;
long topRight = 0;
for (int v : grid[0]) {
topRight += v; // the whole top row, to start
}
long bottomLeft = 0;
long best = Long.MAX_VALUE;
for (int down = 0; down < n; down++) { // the first robot goes down here
topRight -= grid[0][down]; // now only the cells right of down
long second = Math.max(topRight, bottomLeft);
best = Math.min(best, second);
bottomLeft += grid[1][down]; // ready for the next column
}
return best;
}
public static void main(String[] args) {
System.out.println(gridGame(new int[][] {{2, 5, 4}, {1, 5, 1}}));
System.out.println(gridGame(new int[][] {{3, 3, 1}, {8, 5, 2}}));
System.out.println(gridGame(new int[][] {{1, 3, 1, 15}, {1, 3, 3, 1}}));
System.out.println(gridGame(new int[][] {{1, 5, 6}, {6, 6, 1}}));
System.out.println(gridGame(new int[][] {{5}, {7}}));
int[] row = new int[50000];
Arrays.fill(row, 100000);
System.out.println(gridGame(new int[][] {row, row}));
}
}
Running it prints:
4
4
7
6
0
2500000000
The first three lines, 447[[1, 5, 6], [6, 6, 1]]
Look at the last line, the big grid. The answer, 2,500,000,000, is itself too big for an intint
The original implementation,
Complexity
The work is one pass to add up the top row, then one pass over the columns.
- Time: O(n), where n is the number of columns. Each pass visits every column once, with a few additions and comparisons per column.
- Space: O(1). The method keeps three
totals and the loop index. The running totals replace arrays of prefix and suffix sums.long
Splitting an array at each position into a sum before and a sum after is also the heart of