222 - 486 预测赢家

题目

给定一个表示分数的非负整数数组。 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,……。每次一个玩家只能拿取一个分数,分数被拿取之后不再可取。直到没有剩余分数可取时游戏结束。最终获得分数总和最多的玩家获胜。

给定一个表示分数的数组,预测玩家1是否会成为赢家。你可以假设每个玩家的玩法都会使他的分数最大化。

示例 1:

输入: [1, 5, 2] 输出: False 解释: 一开始,玩家1可以从1和2中进行选择。 如果他选择2(或者1),那么玩家2可以从1(或者2)和5中进行选择。如果玩家2选择了5,那么玩家1则只剩下1(或者2)可选。 所以,玩家1的最终分数为 1 + 2 = 3,而玩家2为 5。 因此,玩家1永远不会成为赢家,返回 False。

示例 2:

输入: [1, 5, 233, 7] 输出: True 解释: 玩家1一开始选择1。然后玩家2必须从5和7中进行选择。无论玩家2选择了哪个,玩家1都可以选择233。 最终,玩家1(234分)比玩家2(12分)获得更多的分数,所以返回 True,表示玩家1可以成为赢家。

注意:

  1. 1 <= 给定的数组长度 <= 20.

  2. 数组里所有分数都为非负数且不会大于10000000。

  3. 如果最终两个玩家的分数相等,那么玩家1仍为赢家。

解答

https://leetcode-cn.com/problems/predict-the-winner/solution/java-dong-tai-gui-hua-by-zxy0917-2/

class Solution:
    def PredictTheWinner(self, nums: List[int]) -> bool:
        n = len(nums)
        dp = [[0 for _ in range(n)] for _ in range(n)]
        for i in range(n):
            dp[i][i] = nums[i]
        for i in range(n)[::-1]:
            for j in range(i+1, n):
                dp[i][j] = max(nums[i]-dp[i+1][j], nums[j]-dp[i][j-1])
        return dp[0][n-1] >= 0

Runtime: 24 ms, faster than 97.70% of Python3 online submissions for Predict the Winner.

Memory Usage: 12.8 MB, less than 100.00% of Python3 online submissions for Predict the Winner.

另一种做法:

https://leetcode.com/problems/predict-the-winner/discuss/96828/JAVA-9-lines-DP-solution-easy-to-understand-with-improvement-to-O(N)-space-complexity.

class Solution:
    def PredictTheWinner(self, nums: List[int]) -> bool:
        n = len(nums)
        dp = [[0 for _ in range(n)] for _ in range(n)]
        for i in range(n):
            dp[i][i] = nums[i]
        for l in range(1, n):
            for i in range(n-l):
                j = i+l
                dp[i][j] = max(nums[i]-dp[i+1][j], nums[j]-dp[i][j-1])
        return dp[0][n-1] >= 0

Runtime: 28 ms, faster than 91.01% of Python3 online submissions for Predict the Winner.

Memory Usage: 12.7 MB, less than 100.00% of Python3 online submissions for Predict the Winner.

自顶向下

https://leetcode.com/problems/predict-the-winner/discuss/155217/Top-down-DP-with-Memorization-(Java-Python)

class Solution:
    def PredictTheWinner(self, nums: List[int]) -> bool:
        n = len(nums)
        memo = [[-1 for _ in range(n)] for _ in range(n)]

        def predict(i, j):
            if i > j:
                return 0
            elif i == j:
                return nums[i]
            if memo[i][j] != -1:
                return memo[i][j]
            curScore = max(nums[i]+min(predict(i+2, j), predict(i+1, j-1)),
                           nums[j]+min(predict(i, j-2), predict(i+1, j-1)))
            memo[i][j] = curScore
            return curScore

        scoreFirst = predict(0, n-1)
        scoreTotal = sum(nums)
        return scoreFirst >= scoreTotal - scoreFirst

Runtime: 36 ms, faster than 43.09% of Python3 online submissions for Predict the Winner.

Memory Usage: 12.9 MB, less than 100.00% of Python3 online submissions for Predict the Winner.

Last updated

Was this helpful?