• 力扣-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 。

    方法一:递归

    class Solution(object):
        def PredictTheWinner(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            p1_score = max_score(nums, 0, len(nums) - 1)
            p2_score = sum(nums) - p1_score
            return p1_score >= p2_score
    
    def max_score(li, left, right):
        if left == right:
            return li[left]
        if right - left == 1:
            return max(li[left], li[right])
        if right - left > 1:
            m1_score = li[left] + min(max_score(li, left + 2, right), max_score(li, left + 1, right - 1))
            m2_score = li[right] + min(max_score(li, left + 1, right - 1), max_score(li, left, right - 2))
            return max(m1_score, m2_score)class Solution(object):
        def PredictTheWinner(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            p1_score = max_score(nums, 0, len(nums) - 1)
            p2_score = sum(nums) - p1_score
            return p1_score >= p2_score
    
    def max_score(li, left, right):
        if left == right:
            return li[left]
        if right - left == 1:
            return max(li[left], li[right])
        if right - left > 1:
            m1_score = li[left] + min(max_score(li, left + 2, right), max_score(li, left + 1, right - 1))
            m2_score = li[right] + min(max_score(li, left + 1, right - 1), max_score(li, left, right - 2))
            return max(m1_score, m2_score)

    方法二:递归-优化

    存储先手和后手的得分差值

    class Solution(object):
        def PredictTheWinner(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            diff_score = max_score(nums, 0, len(nums) - 1)
            return diff_score >= 0
    
    def max_score(li, left, right):
        if left == right:
            return li[left]
        m1_score = li[left] - max_score(li, left+1, right)
        m2_score = li[right] - max_score(li, left, right-1)
        return max(m1_score, m2_score)

    方法三:动态规划

    class Solution(object):
        def PredictTheWinner(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
            length = len(nums)
            dp = [[0] * length for _ in range(length)]
            for i, item  in enumerate(nums):
                dp[i][i] = item
            for i in range(length-2, -1, -1):
                for j in range(i+1, length):
                    dp[i][j] = max(nums[i] - dp[i+1][j], nums[j] - dp[i][j-1])
            return dp[0][length-1] >= 0
    时刻记着自己要成为什么样的人!
  • 相关阅读:
    PTA(Advanced Level)1037.Magic Coupon
    PTA(Advanced Level)1033.To Fill or Not to Fill
    PTA(Basic Level)1020.月饼
    PTA(Advanced Level)1048.Find Coins
    PTA(Advanced Level)1050.String Subtraction
    PTA(Advanced Level)1041.Be Unique
    PTA(Basci Level)1043.输出PATest
    PTA(Basic Level)1039.到底买不买
    PTA(Basic Level)1033.旧键盘打字
    PTA(Advanced Level)1083.List Grades
  • 原文地址:https://www.cnblogs.com/demo-deng/p/14771207.html
Copyright © 2020-2023  润新知