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

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/predict-the-winner/description/

题目描述:

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.

题目大意

眼神越来越不好了,看了很多遍题目都看成了预测冬天-_-||

题目的意思很简单,两个玩家可以轮流的从一个数组前面和后面拿数字,每次只能拿一个,被拿走的不能再拿了,最后判断先拿的那个玩家能不能赢。

解题方法

这个题要感谢左程云的讲解,使用两个函数代表先发和后发的情况。如果先发,那么应该返回的是拿走前面的数字或者拿走后面的数字能拿到的结果的最大值。但是如果后发,那么应该返回的是前面不拿和后面不拿的最小值。

直接暴力递归会超时,使用记忆化搜索就能通过了。因为使用的python2,除法默认是地板除,这就是我第一次提交失败的原因,这种情况使用浮点数除法就没问题了。

题目和877. Stone Game做法一样的。

官方解答写了各种方法的对比,包括DP,值得一看:https://leetcode.com/articles/predict-the-winner/

代码如下:

class Solution(object):
def PredictTheWinner(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
_sum = sum(nums)
self.f_map, self.s_map = dict(), dict()
player1 = self.f(nums, 0, len(nums)-1, self.f_map)
print(player1)
return player1 >= _sum / 2.0 def f(self, nums, start, end, f_map):
if start == end:
return nums[start]
if (start, end) not in f_map:
f_val = max(nums[start] + self.s(nums, start+1, end, self.s_map), nums[end] + self.s(nums, start, end-1, self.s_map))
f_map[(start, end)] = f_val
return f_map[(start, end)] def s(self, nums, start, end, s_map):
if start == end:
return 0
if (start, end) not in s_map:
s_val = min(self.f(nums, start+1, end, self.f_map), self.f(nums, start, end-1, self.f_map))
s_map[(start, end)] = s_val
return s_map[(start, end)]

上面的代码写复杂了,可以简化成下面这样:

class Solution(object):
def PredictTheWinner(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
_sum = sum(nums)
self.f_map, self.s_map = dict(), dict()
player1 = self.f(nums, 0, len(nums)-1)
return player1 >= _sum / 2.0 def f(self, nums, start, end):
if start == end:
return nums[start]
if (start, end) not in self.f_map:
f_val = max(nums[start] + self.s(nums, start+1, end), nums[end] + self.s(nums, start, end-1))
self.f_map[(start, end)] = f_val
return self.f_map[(start, end)] def s(self, nums, start, end):
if start == end:
return 0
if (start, end) not in self.s_map:
s_val = min(self.f(nums, start+1, end), self.f(nums, start, end-1))
self.s_map[(start, end)] = s_val
return self.s_map[(start, end)]

方法二:

DP,未完待续。

参考资料:

日期

2018 年 9 月 8 日 ———— 美好的周末,从刷题开始

【LeetCode】486. Predict the Winner 解题报告(Python)的更多相关文章

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

  2. [LeetCode] 486. Predict the Winner 预测赢家

    Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from eith ...

  3. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  4. 随手练——博弈论入门 leetcode - 486. Predict the Winner

    题目链接:https://leetcode.com/problems/predict-the-winner/ 1.暴力递归 当前数组左边界:i,右边界:j: 对于先发者来说,他能取到的最大值是:max ...

  5. [leetcode] 486. Predict the Winner (medium)

    原题 思路: 解法一: 转换比较拿取分数多少的思路,改为考虑 player拿的分数为正,把Player2拿的视为负,加上所有分数,如果最后结果大于0则Player1赢. 思考得出递归表达式: max( ...

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

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

  7. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  8. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  9. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

随机推荐

  1. EXCEL-批量删除筛选出的行,并且保留首行

    筛选->ctrl+G->可见单元格->鼠标右键->删除整行. 之前的时候,是有个方法类似于上述步骤,可以保留标题行的,但是,不知道是不是少了哪一步,上述过程总是会删除标题行.就 ...

  2. 16. Linux find查找文件及文件夹命令

    find的主要用来查找文件,查找文件的用法我们比较熟悉,也可用它来查找文件夹,用法跟查找文件类似,只要在最后面指明查找的文件类型 -type d,如果不指定type类型,会将包含查找内容的文件和文件夹 ...

  3. 【leetcode】36. Valid Sudoku(判断能否是合法的数独puzzle)

    Share Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated accordi ...

  4. linux 6.5 网卡

    启动网卡 ifup eth0 eth0:网卡名称 设置网卡开机启动 vi /etc/sysconfig/network-scripts/ifcfg-eth0 ONBOOT=yes

  5. tomcat 之 session服务器 (memcache)

    #: 在tomcat各节点安装memcached [root@node1 ~]# yum install memcached -y #: 下载tomcat所需的jar包(此处在视频中找软件) [roo ...

  6. 快速上手git gitlab协同合作

    简单记录,整理. 摘要 为方便大家快速上手Git,并使用Gitlab协同合作,特编写此手册,手册内容不会太丰富与深入.主要包含如下内容: Git 使用教程1.1 安装1.2 常用命令1.3 版本控制1 ...

  7. spring boot 启动卡半天

    测试服务器到期,把环境切了,早上过来 ios 和 安卓 都说 测试环境连不上,ps -ef | grep app.jar 查看了一下进程,发现没有启动,于是 重新打包.部署,一顿骚操作后,监控启动日志 ...

  8. spring boot 配置属性值获取注解@Value和@ConfigurationProperties比较

    功能比较 :     @ConfigurationProperties  @Value  映射赋值 批量注入配置文件中的属性 一个个指定 松散绑定(松散语法)① 支持 不支持 SpEL② 不支持 支持 ...

  9. 【手帐】Bullet Journal教程

    最近觉得自己的日程记录本有待提高,于是从今年开始开始入坑了手帐. *内容源自Bullet Journal官网.https://bulletjournal.com/pages/learn 快速笔记 Bu ...

  10. Apifox(1)比postman更优秀的接口自动化测试平台

    Apifox介绍 Apifox 是 API 文档.API 调试.API Mock.API 自动化测试一体化协作平台,定位 Postman + Swagger + Mock + JMeter.通过一套系 ...