【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. Flume消费内外网分流配置的Kafka时遇到的坑

    网上有铺天盖地的文章,介绍如何将Kafka同时配置成公网地址.内网地址,以实现内外网分流,看着都很成功. 但我们通过Flume消费一个配置了内外网分流的Kafka(版本0.10.1)集群时遇到了坑,却 ...

  2. javaSE高级篇3 — 网络编程 — 更新完毕

    网络编程基础知识 先来思考两个问题( 在这里先不解决 ) 如何准确的找到一台 或 多台主机? 找到之后如何进行通讯? 网络编程中的几个要素 IP 和 端口号 网络通讯协议:TCP / UDP 最后一句 ...

  3. 顺序栈(C++)

    栈的定义为只允许在表的末端进行插入和删除的线性表.简而言之就是先进后出的线性表. 插入和删除的一端被称呼为栈顶(top),而不允许插入删除的一端被称为栈底(bottom).无元素时的栈即为空栈. 使用 ...

  4. Swift Storyboard找不到类文件

    Swift语言引入了Module概念,在通过关键字@objc(类名)做转换的时候,由于Storyboard没有及时更新Module属性,会导致如下两种类型错误: 1 用@objc(类名)标记的Swif ...

  5. 远程连接mysql库问题

    如果你想连接你的mysql的时候发生这个错误: ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL serve ...

  6. 【Linux】【Basis】网络

    Linux网络属性配置                           计算机网络:          TCP/IP:协议栈(使用)             ISO,OSI:协议栈(学习)     ...

  7. TCP协议三步挥手与四步挥手

    关于TCP协议 TCP(Transmission Control Protocol, 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议.与之对应的是UDP(User Datagram ...

  8. CSS font-size: 0去除内联元素空白间隙

    我们在编写HTML标签的时候,通常会使用换行,缩进来保证代码的可读性.同时,在编写CSS样式的时候,也会需要把一些元素设置为inline或inline-block.这样一来,有时在页面中会出现意外的空 ...

  9. Spring Boot项目的不同启动方式

    方式一: 直接通过IntelliJ IDEA启动,直接执行Spring Boot项目的main()方法. 方法二: 将项目打包成jar包,首先需要在pom.xml文件的根节点下添加如下配置: < ...

  10. Spring Cloud Alibaba 整合 Nacos 实现服务配置中心

    在之前的文章 <Nacos 本地单机版部署步骤和使用> 中,大家应该了解了 Nacos 是什么?其中 Nacos 提供了动态配置服务功能 一.Nacos 动态配置服务是什么? 官方是这么说 ...