leetcode 1140. Stone Game II

Meaning of the questions:

Alex and Lee continue their game stones. Many stones piled in a row, each stack has a positive integer stone pieces piles [i]. Who's Got the game with the most stones to determine the winner.
Alex and Lee take turns, Alex first start. Initially, M = 1.
In each player's turn, the player can take away all the rest of the stone heap before X, where 1 <= X <= 2M. Then, so that M = max (M, X) .
The game continues until all the stones have been taken away.
Alex Lee assumptions and play the best level, returns the largest number of stone Alex can get.

Ideas:

Because two people have to play the best level, so that the same policy, the purpose is to make up their own after as little as possible, you can do with dp.

Memory search, DP [i] [j] denotes the i-th time taken from the start to the maximum M j, for each state, and subtracting the suffix enumerated next state, the current state of the maximum is found,dp[i][j]=max(dp[i][j],sum[i]-dp[i+x][max(j,x)]);

Code:

 class Solution {
public:
    int dp[110][110],sum[110];
    int stoneGameII(vector<int>& piles) {
        if(piles.size()==0)return 0;
        int n=piles.size();
        for(int i=n-1;i>=0;i--)sum[i]=sum[i+1]+piles[i];
        for(int i=0;i<n;i++){
            for(int j=1;j<=n;j++){
                if(i+2*j>=n)dp[i][j]=sum[i];
            }
        }
        for(int i=n-1;i>=0;i--){
            for(int j=n;j>=1;j--){
                for(int x=1;x<=2*j&&i+x<n;x++){
                    dp[i][j]=max(dp[i][j],sum[i]-dp[i+x][max(j,x)]);
                }
            }
        }
        return dp[0][1];
    }
};

Guess you like

Origin www.cnblogs.com/ljy08163268/p/11575207.html