博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】486. Predict the Winner
阅读量:5943 次
发布时间:2019-06-19

本文共 1924 字,大约阅读时间需要 6 分钟。

题目如下:

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: FalseExplanation: 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: TrueExplanation: 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. 1 <= length of the array <= 20.
  2. Any scores in the given array are non-negative integers and will not exceed 10,000,000.
  3. If the scores of both players are equal, then player 1 is still the winner.

解题思路:和题一样。

代码如下:

class Solution(object):    def PredictTheWinner(self, nums):        """        :type nums: List[int]        :rtype: bool        """        dp = []        for i in range(len(nums)):            dp.append([0] * len(nums))            dp[i][i] = nums[i]        for i in range(1,len(nums)):            for j in range(len(nums) - i):                # range is j -> j + i                dp[j][j+i] = max(nums[j] - dp[j+1][j+i], nums[j+i] - dp[j][j+i-1])        #print dp        return dp[0][-1] >= 0

 

转载于:https://www.cnblogs.com/seyjs/p/10944002.html

你可能感兴趣的文章
Docker镜像
查看>>
打印HotSpot VM采用自动优化参数
查看>>
install mysql with source code
查看>>
OC语言的代码保护
查看>>
IBM磁带库中更换磁带的步骤
查看>>
tomcat启动报错
查看>>
mybatis3单表增删改查(二)——注解方式
查看>>
【Linux基础】作业二
查看>>
SQL0332N 不支持从源代码页 "XXXX" 到目标代码页 "XXXX"
查看>>
【存储过程】从数据库中读取数据保存到文件中
查看>>
购华为第1书,写书评赢大奖
查看>>
Linux系统程序包管理工具 RPM
查看>>
朱翊:从鼎级云珍冰箱看卡萨帝的百年品牌逻辑
查看>>
进军“手机照相馆”:京东要和3C厂商干什么?
查看>>
多数大数据项目都以失败而告终的原因
查看>>
再谈MySQL JSON数据类型
查看>>
单色打印
查看>>
一生只见一次的大彗星今天来了!
查看>>
设置root密码
查看>>
SQLPLUS SPOOL命令使用详解
查看>>