博弈问题

486. Predict the Winner

题目链接
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.

Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.

Example 1:

Input: [1, 5, 2]
Output: False
Explanation: Initially, player 1 can choose between 1 and 2. 
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2). 
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5. 
Hence, player 1 will never be the winner and you need to return False.

Example 2:

Input: [1, 5, 233, 7]
Output: True
Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.

Note:

1 <= length of the array <= 20.
Any scores in the given array are non-negative integers and will not exceed 10,000,000.
If the scores of both players are equal, then player 1 is still the winner.

题目大意
有一个整数数组代表分数,两个玩家。他们分别从数组两端取分数,最后谁总共得到的分数最大谁就是赢家。求先手玩家是否能赢?

解题思路
状态
dp[i][j] :在数组[i, j]中玩家1能得的最大分
玩家1从左端取数: nums[i] - dp[i +1][j]
玩家2从右端取数: nums[j] - dp[i][j - 1]

取值
以上两者最大值

转移方程
dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]);
遍历从对角线到右上角

复杂度

public boolean PredictTheWinner(int[] nums) {
    if (nums == null || nums.length == 0){
        return true;
    }
    
    int[][] dp = new int[nums.length][nums.length];
    
    for (int i = 0; i < nums.length; i++){
        dp[i][i] = nums[i];
    }
    
    
    for (int i = nums.length - 2; i >= 0; i--){
        for (int j = i + 1; j < nums.length; j ++){
            dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]);
        }
    }
    
    return dp[0][nums.length - 1] >= 0;
}

相似题目
877. Stone Game


1140. Stone Game II

Alex and Lee 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.

Alex and Lee take turns, with Alex 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 Alex and Lee play optimally, return the maximum number of stones Alex can get.

Example 1:

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

Constraints:

1 <= piles.length <= 100
1 <= piles[i] <= 10 ^ 4

题目大意
有两个玩家,许多堆石子 排成一行,每堆都有正整数颗石子 piles[i]。游戏以谁手中的石子最多来决出胜负。
玩家轮流取石子,亚历克斯先开始。最初,M = 1。
在每个玩家的回合中,该玩家可以拿走剩下的 前 X 堆的所有石子,其中 1 <= X <= 2M。然后,令 M = max(M, X)。
游戏一直持续到所有石子都被拿走。
假设亚历克斯和李都发挥出最佳水平,返回亚历克斯可以得到的最大数量的石头。

解题思路
状态
dp[i][j] : 数f(i,M),表示在piles[i:]中以[1,2*M]为取值范围可以取的最多石子数

转移方程
dp[i][M] = Math.max(dp[i][M], sum[i] - dp[i + X][Math.max(M, X)]);

all the left stones - the stones next player can get


复杂度
SC: O(n^2)
TC: O(n^3)

public int stoneGameII(int[] piles) {
    if (piles == null || piles.length == 0) return 0;
    int n = piles.length;
    
    int[] sum = new int[n+1];
    for (int i = n - 1; i >= 0; i--){ 
    	sum[i] += sum[i + 1] + piles[i];
    }
    
    int[][] dp = new int[n + 1][n + 1];
    
    for (int i = n - 1; i >= 0; i--){
        for (int M = 1; M < n; M++){
            for (int X = 1; X <= 2*M && X + i <= n; X++){
                dp[i][M] = Math.max(dp[i][M], sum[i] - dp[i + X][Math.max(X, M)]);
            }
        }
    }
    return dp[0][1];
}

1406. Stone Game III

题目链接
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.

Alice and Bob take turns, with Alice starting first. On each player’s turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.

The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.

The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.

Assume Alice and Bob play optimally.

Return “Alice” if Alice will win, “Bob” if Bob will win or “Tie” if they end the game with the same score.

Example 1:

Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.

Example 2:

Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.

Example 3:

Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.

Example 4:

Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"

Example 5:

Input: values = [-1,-2,-3]
Output: "Tie"

Constraints:

1 <= values.length <= 50000
-1000 <= values[i] <= 1000

题目大意
Alice 和 Bob 用几堆石子在做游戏。几堆石子排成一行,每堆石子都对应一个得分,由数组 stoneValue 给出。

Alice 和 Bob 轮流取石子,Alice 总是先开始。在每个玩家的回合中,该玩家可以拿走剩下石子中的的前 1、2 或 3 堆石子 。比赛一直持续到所有石头都被拿走。

每个玩家的最终得分为他所拿到的每堆石子的对应得分之和。每个玩家的初始分数都是 0 。比赛的目标是决出最高分,得分最高的选手将会赢得比赛,比赛也可能会出现平局。

假设 Alice 和 Bob 都采取 最优策略 。如果 Alice 赢了就返回 “Alice” ,Bob 赢了就返回 “Bob”,平局(分数相同)返回 “Tie” 。

解题思路
状态

  • dp[i] : 从stoneValue[i] 开始算,A能比B多拿多少
  • 拿一堆石子:Take A[i], win take - dp[i+1]
  • 拿两堆: Take A[i] + A[i+1], win take - dp[i+2]
  • 拿三堆:Take A[i] + A[i+1] + A[i+2], win take - dp[i+3]

转移方程

take += A[i + k];
dp[i] = Math.max(dp[i], take - dp[i + k + 1]);

复杂度
TC: O(n^2)
SC: O(n)

public String stoneGameIII(int[] stoneValue) {
    if (stoneValue == null || stoneValue.length == 0) return "Tie";
    
    int n = stoneValue.length;
    int[] dp = new int[n + 1];
    
    
    
    for (int i = n - 1; i >= 0; i--){
        dp[i] = Integer.MIN_VALUE;
        for (int k = 0, take = 0; k < 3 && i + k < n; k++){
            take += stoneValue[i + k];
            dp[i] = Math.max(dp[i], take - dp[i + k + 1]);
        }
    }
    
    if (dp[0] == 0) return "Tie";
    return dp[0] > 0 ? "Alice" : "Bob";
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值