# leetcode 2017.Grid games (C++, java, python)

Give you a two-dimensional array grid with subscripts starting at 0 and size of 2 x n, where grid[r][c] represents the number of points in the position (r, c) of the matrix. Now two robots are playing a game on the matrix.

Both robots start at (0, 0) and target at (1, n-1). Each robot will only go right ((r, c) to (r, C + 1) or down ((r, c) to (r + 1, c).

At the beginning of the game, the first robot moves from (0,0) to (1,n-1) and collects all the points of the cells on the path. For all cells on the path (r,c), the grid[r][c] resets to 0 after the path. Then the second robot moves from (0,0) to (1,n-1)Also collects the total number of cells on the path. Note that there may be intersections in their paths.

The first one wants to hit the competitor and minimize the number of points collected by the second one. In contrast, the second one wants to maximize the number of points collected by itself. Both robots return the number of points collected by the second one, while playing their best role.

Example 1: ```Input: grid = [[2,5,4],[1,5,1]]
Output: 4
Interpretation: The best path for the first robot is shown in red, and the best path for the second is shown in blue.
Cells visited by the first robot will be reset to zero.
The second robot will collect 0 + 0 + 4 + 0 = 4 points.
```

Example 2: ```Input: grid = [[3,3,1],[8,5,2]]
Output: 4
Interpretation: The best path for the first robot is shown in red, and the best path for the second is shown in blue.
Cells visited by the first robot will be reset to zero.
The second robot will collect 0 + 3 + 1 + 0 = 4 points.
```

Example 3: ```Input: grid = [[1,3,1,15],[1,3,3,1]]
Output: 7
Interpretation: The best path for the first robot is shown in red, and the best path for the second is shown in blue.
Cells visited by the first robot will be reset to zero.
The second robot will collect 0 + 1 + 3 + 3 + 0 = 7 points.
```

Tips:

• grid.length == 2
• n == grid[r].length
• 1 <= n <= 5 * 104
• 1 <= grid[r][c] <= 105

C++

```class Solution {
public:
long long gridGame(vector<vector<int>> &grid) {
int n = grid.size();
vector<long> up(n, 0);
vector<long> down(n, 0);
up[n - 1] = grid[n - 1];
down = grid;
for (int i = n - 2; i >= 0; i--) {
up[i] = up[i + 1] + grid[i];
}
for (int i = 1; i < n; i++) {
down[i] = down[i - 1] + grid[i];
}
long res = min(up, down[n - 2]);
for (int i = 1; i < n - 1; i++) {
res = min(res, max(up[i + 1], down[i - 1]));
}
return res;
}
};```

java

```class Solution {
public long gridGame(int[][] grid) {
int n = grid.length;
long[] up = new long[n];
long[] down = new long[n];
up[n - 1] = grid[n - 1];
down = grid;
for (int i = n - 2; i >= 0; i--) {
up[i] = up[i + 1] + grid[i];
}
for (int i = 1; i < n; i++) {
down[i] = down[i - 1] + grid[i];
}
long res = Math.min(up, down[n - 2]);
for (int i = 1; i < n - 1; i++) {
res = Math.min(res, Math.max(up[i + 1], down[i - 1]));
}
return res;
}
}```

python

```class Solution:
def gridGame(self, grid: List[List[int]]) -> int:
n = len(grid)
up =  * n
down =  * n
up[n - 1] = grid[n - 1]
down = grid
for i in range(n - 2, -1, -1):
up[i] += up[i + 1] + grid[i]
for i in range(1, n):
down[i] += down[i - 1] + grid[i]
res = min(up, down[n - 2])
for i in range(1, n - 1):
res = min(res, max(up[i + 1], down[i - 1]))
return res```

Posted on Wed, 29 Sep 2021 12:11:42 -0400 by wanttoshop