题目如下:

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. 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.

解题思路:【leetcode】877. Stone Game题一样。

代码如下:

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

【leetcode】486. Predict the Winner的更多相关文章

  1. 【LeetCode】486. Predict the Winner 解题报告(Python)

    [LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  2. LN : leetcode 486 Predict the Winner

    lc 486 Predict the Winner 486 Predict the Winner Given an array of scores that are non-negative inte ...

  3. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. 源码编译apache设置系统启动失败

    文章为转载,亲试成功. Apache无法自动启动,1.将apachectl文件拷贝到/etc/rc.d/init.d 中,然后在/etc/rc.d/rc5.d/下加入链接即可.命令如下:cp /usr ...

  2. What is the difference between Kill and Kill -9 command in Unix?

    w difference kill -9 pid and kill pid command - Ask Ubuntu  https://askubuntu.com/questions/791841/d ...

  3. (转)flexpaper 参数

    本文转载自:http://blog.csdn.net/z69183787/article/details/18659913 Flexpaper可能用到如下参数   SwfFile (String) 需 ...

  4. list转datatable,SqlBulkCopy将DataTable中的数据批量插入数据库

    /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...

  5. 阶段1 语言基础+高级_1-2 -面向对象和封装_16this关键字的作用

    this主要是在重名的情况下 ,起到区分的效果 新建demo04的包,里面新建类Person 通过this.进行区分 this关键字可以解决重名 分不开的问题 这里的person调用的sayHello ...

  6. DataFrame 结构

    概念 DataFrame 是表格型的数据结构 ,DataFrame 本质上可以看做是由series 组成的字典, 它既有行索引,也有列索引.  它并不是列表,也不是字典,.

  7. altium学习之常用快捷键

    1.放大缩小:常用方法,ctrl+鼠标滚轮,鼠标中键+移动鼠标,pgup.pgup. 2.切换不同的布线层:ctrl+shift+鼠标滚轮 3.在SCH或者PCB 同一平面内左右翻转:ctrl+X 4 ...

  8. TCP为什么要三次握手和四次挥手

    http://www.jellythink.com/archives/705 简析TCP的三次握手与四次分手 https://zhuanlan.zhihu.com/p/24001696 计算机网络面试 ...

  9. ntp局域网时间同步操作

    需求:局域网里面有两台电脑需要同步时间 一台windows,一台Linux.把windows当作服务器 windows10自带ntp服务器,可以按如下步骤进行设置 1. 打开注册表编辑器,在运行里面输 ...

  10. 总结:String类型与Int类型的转换【实现插入操作主键自增】

    1.String类型(此类型是数字格式的字符串类型)转换成Int类型 String str = "10000"; 转换成Int类型: int num = Integer.parse ...