Leetcode 5379. pebble game III (183 games week race)

Title Contents

Alice and Bob heap with a few stones in the game. Piles of stones in a row, each corresponding to one stack stones score is given by an array stoneValue.

Alice and Bob take turns stones, Alice always first start. In each player's turn, the player can take the rest of the stones in front of the stone heap 1, 2 or 3. The game continues until all the stones have been taken away.

The final score for each player corresponding to each pile he got the stones and the score. The initial score for each player is 0. The object of the game is decided at the highest points, the highest score player will win the game, the game may also be a draw.

Suppose Alice and Bob have taken the best strategy. If Alice wins on return to "Alice", Bob won on return "Bob", (same score) draw return "Tie".

Source: stay button (LeetCode)
link: https://leetcode-cn.com/problems/stone-game-iii

Topic analysis

This question is in fact now take both ends of the array and the number of very similar, are both parties to choose their own optimal solution, so we can still use a similar dynamic programming to solve problems.

Problem-solving ideas

Since both sides are optimal, if we analyze from front to back, the two sides each have three options strategy is difficult to know the other side of the optimal solution after selecting a policy, it is difficult to analyze it. So we need to move forward from the analysis in accordance with the order of the stones, pour the beginning of the introduction of optimal state.

State indicates: Order sequence dp [i] representative of the i-th to the last pebble stone consisting of, current players take stones maximum obtainable.

Because it is fixed from i to score the last stone, it is the score of all the stones and, while the scores of two players and a total score of stones, gravel so in order to take the current highest score is equivalent to taking the stones after the lowest score of the other side. The current take stones 1,2,3 three kinds of borrowing, so the other side of the optimal solution if we know these three emulated took after, then we choose the optimal solution other kind of borrowing to a minimum. State transition equation is as follows :

// sum[i]为第i个石子到最后一个石子的总得分
dp[i] = sum[i] - min(dp[i+1], dp[i+2], dp[i+3]);

With the state transition equation, and then note the boundary conditions can be.

AC Code

class Solution {
public:
    string stoneGameIII(vector<int>& stoneValue) {
        int n = stoneValue.size();
        int dp[n];
        // res[i] 表示从第i个石子到最后一个石子的得分之和
        int res[n];
        res[n-1] = stoneValue[n-1];
        for(int i=n-2;i>=0;--i){
            res[i] = res[i+1]+stoneValue[i];
        }
        //dp[n-1] = res[n-1];
        for(int i=n-1;i>=0;--i){
                int minv;
                // 这里表示如果剩下的石子小于等于3
                // 该选手可以一次把所有石子取完
                // minv = 0就表示另一个选手没有石子可以取
                if(i+3>=n) minv = 0;
                else minv = INT_MAX;
                
                if(i+1<n){
                    minv = min(minv, dp[i+1]);
                }
                if(i+2<n){
                    minv = min(minv, dp[i+2]);
                }
                if(i+3<n){
                    minv = min(minv, dp[i+3]);
                }
                dp[i] = res[i]-minv;
        }
        if(res[0]-dp[0]>dp[0]) return "Bob";
        else if(res[0]-dp[0] == dp[0]) return "Tie";
        else return "Alice";
    }
};

Guess you like

Origin www.cnblogs.com/wenxuanh/p/12636746.html