Description Link to heading

1140. Stone Game II (Medium)

Alice and Bob continue their games with piles of stones. There are a number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]. The objective of the game is to end with the most stones.

Alice and Bob take turns, with Alice starting first. Initially, M = 1.

On each player’s turn, that player can take all the stones in the first X remaining piles, where 1 <= X <= 2M. Then, we set M = max(M, X).

The game continues until all the stones have been taken.

Assuming Alice and Bob play optimally, return the maximum number of stones Alice can get.

Example 1:

Input: piles = [2,7,9,4,4]
Output: 10
Explanation:  If Alice takes one pile at the beginning, Bob takes two piles, then Alice takes 2
piles again. Alice can get 2 + 4 + 4 = 10 piles in total. If Alice takes two piles at the beginning,
then Bob can take all three piles left. In this case, Alice get 2 + 7 = 9 piles in total. So we
return 10 since it's larger.

Example 2:

Input: piles = [1,2,3,4,5,100]
Output: 104

Constraints:

  • 1 <= piles.length <= 100
  • 1 <= piles[i] <= 10⁴

Solution Link to heading

The meaning of playing optimally is if one get first x stones, the number of the other can get is minmum.

If this point is noted, we can use memorized search to solve the problem, besides, we need to use suffix sum array.

We can transform memorized search to dynamic programming.

Code Link to heading

class Solution {
public:
    int dfs(int idx_start, int M, vector<int> &postfix, int n, vector<vector<int>> &cach) {
        if (idx_start >= n)
            return 0;
        
        int minnum = 100001;
        if (cach[idx_start][M] >= 0) {
            return cach[idx_start][M];
        }
        for (int i = idx_start + 1; i <= idx_start +  2 * M && i <= n; i++) { 
            int tmp = dfs(i, std::max(i - idx_start, M), postfix, n, cach);
            if (minnum > tmp) {
                minnum = tmp;
            }
        }
        cach[idx_start][M] = postfix[idx_start] - minnum;
        return cach[idx_start][M];
         
    }
    int stoneGameII(vector<int>& piles) {
        int n = piles.size();
        vector<int> postfix(n + 1, 0);
        for (int i = n - 1; i >= 0; i--) {
            postfix[i] = postfix[i + 1] + piles[i];
        }
        vector<vector<int>> cach(n + 1, vector<int>(n, -1));
        return dfs(0, 1, postfix, n, cach);
    }
};

Dynamic programming Link to heading

class Solution {
  public:
    int stoneGameII(vector<int> &piles) {
        int n = piles.size();
        vector<int> postfix(n + 1, 0);
        for (int i = n - 1; i >= 0; i--) {
            postfix[i] = postfix[i + 1] + piles[i];
        }
        vector<vector<int>> dp(n + 1, vector<int>(n + 1, 0));
        for (int i = n - 1; i >= 0; i--) {
            for (int M = 1; M <= i / 2 + 1; M++) {
                if (i + 2 * M  >= n) { 
                    dp[i][M] = postfix[i];
                } else {
                    int min_num = INT_MAX;
                    for (int x = 1; x <= 2 * M; x++) {
                        min_num = std::min(min_num, dp[i + x][std::max(M, x)]); 
                    }
                    dp[i][M] = postfix[i] - min_num;
                }
            }
        }
        return dp[0][1];
    }
};