Similar Problems

Similar Problems not available

Grid Game - Leetcode Solution

Companies:

LeetCode:  Grid Game Leetcode Solution

Difficulty: Medium

Topics: matrix prefix-sum array  

Problem Statement:

Grid Game is a problem on leetcode under the section Hard. It involves a 2D grid of size m x n and two players, player 1 and player 2.

At the beginning of the game, player 1 places a stone at any cell of the grid. Then, the two players take turns playing the game. In each turn, a player must choose one of their stones and move it to an adjacent cell in the same row or column. A player can't move the stone to a cell where another stone already exists. Also, player 1 can't move a stone to a cell where he has already placed a stone before.

The game continues until the player 2 can no longer make any move. The score of player 1 is the sum of values in all the cells of the grid where he has placed his stones. The score of player 2 is the sum of values in all the cells of the grid where he has placed his stones.

The problem is to find the maximum score that player 1 can obtain, assuming both players play optimally. The grid is represented by a 2D array of integers called 'grid', where grid[i][j] represents the value of the cell at position (i, j).

Solution Approach:

We will solve the Grid Game problem by using dynamic programming.

We will consider two states of the game. The first state is when it is player 1's turn to play, and the second state is when it is player 2's turn to play.

In the first state, we will find the maximum score that player 1 can obtain by considering all the possible moves that he can make from his current position. We will then subtract this value from the total sum of values in the grid to get the score of player 2. We will store these values in a 2D array called 'dp1'.

In the second state, we will find the maximum score that player 2 can obtain by considering all the possible moves that he can make from his current position. We will then subtract this value from the total sum of values in the grid to get the score of player 1. We will store these values in a 2D array called 'dp2'.

Finally, we will iterate through all the positions of the grid and find the maximum value of dp1[i][j] + dp2[i][j-1] and dp1[i][j-1] + dp2[i][j], since player 1 can place a stone at position (i, j) or at position (i, j-1), and player 2 can place a stone at position (i, j) or at position (i, j-1) respectively.

The maximum of all these values will be the maximum score that player 1 can obtain.

Code:

The code for Grid Game problem is given below:

class Solution {
public:
    long long gridGame(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        long long dp1[m][n], dp2[m][n];
        memset(dp1, 0, sizeof dp1);
        memset(dp2, 0, sizeof dp2);
        dp1[0][0] = grid[0][0];
        dp2[0][0] = 0;
        for(int j = 1; j < n; j++){
            dp1[0][j] = dp1[0][j-1] + grid[0][j];
        }
        for(int j = 1; j < n; j++){
            dp2[0][j] = dp2[0][j-1] + grid[0][j-1];
        }
        for(int i = 1; i < m; i++){
            dp1[i][0] = dp1[i-1][0] + grid[i][0];
        }
        for(int i = 1; i < m; i++){
            dp2[i][0] = dp2[i-1][0] + grid[i-1][0];
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp1[i][j] = max(dp1[i-1][j], dp1[i][j-1]) + grid[i][j];
            }
        }
        for(int i = m-2; i >= 0; i--){
            for(int j = n-2; j >= 0; j--){
                dp2[i][j] = max(dp2[i+1][j], dp2[i][j+1]) + grid[i][j];
            }
        }
        long long ans = LLONG_MAX;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(i == 0 && j == 0) continue;
                long long s1 = 0, s2 = 0;
                if(i > 0) s1 += dp1[i-1][j];
                if(j > 0) s1 += dp1[i][j-1];
                if(i < m-1) s2 += dp2[i+1][j];
                if(j < n-1) s2 += dp2[i][j+1];
                ans = min(ans, max(s1, s2));
            }
        }
        return ans;
    }
};

Time Complexity: O(m*n)

Space Complexity: O(m*n)

Grid Game Solution Code

1