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[0].size();
        vector<long> up(n, 0);
        vector<long> down(n, 0);
        up[n - 1] = grid[0][n - 1];
        down[0] = grid[1][0];
        for (int i = n - 2; i >= 0; i--) {
            up[i] = up[i + 1] + grid[0][i];
        }
        for (int i = 1; i < n; i++) {
            down[i] = down[i - 1] + grid[1][i];
        }
        long res = min(up[1], 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[0].length;
        long[] up = new long[n];
        long[] down = new long[n];
        up[n - 1] = grid[0][n - 1];
        down[0] = grid[1][0];
        for (int i = n - 2; i >= 0; i--) {
            up[i] = up[i + 1] + grid[0][i];
        }
        for (int i = 1; i < n; i++) {
            down[i] = down[i - 1] + grid[1][i];
        }
        long res = Math.min(up[1], 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[0])
        up = [0] * n
        down = [0] * n
        up[n - 1] = grid[0][n - 1]
        down[0] = grid[1][0]
        for i in range(n - 2, -1, -1):
            up[i] += up[i + 1] + grid[0][i]
        for i in range(1, n):
            down[i] += down[i - 1] + grid[1][i]
        res = min(up[1], down[n - 2])
        for i in range(1, n - 1):
            res = min(res, max(up[i + 1], down[i - 1]))
        return res

Tags: Python C++ leetcode greedy algorithm

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