877. Stone Game
问题
有偶数堆石头(数组长度为偶数),每堆石头有一些石头(数组元素为正),石头的总数是奇数。Alex和Lee两个人轮流取石头堆,每次可以从头部或尾部取,Alex先取。
给定这样一个数组,两人都以最优策略去玩这个游戏,如果Alex一定会获胜则返回True,否则返回False
思路
做法1:因为有偶数堆,所以先玩游戏的Alex可以做到:一直取第偶数堆,或者一直取第奇数堆。石头总数是奇数,那么第偶数堆的石头总数和第奇数堆的石头总数,一定有一方大于另一方。所以Alex可以计算出哪方大来取。假设第奇数堆的石头总数大于第偶数堆的石头总数,Alex就一直取第奇数堆来获胜。所以Alex总能获胜,直接return True即可。
时间复杂度O(1),空间复杂度O(1)
做法2:也可以用dp的做法来做。由于是一个双方博弈的游戏,dp表示单方的获取价值(石头)不好做,我们定义dp为双方的获取价值的差,即“Alex比Lee多获得的价值”。
对于Alex而言,他要使得dp值最大,用dp[i][j]表示从i到j的石头堆中Alex比Lee多获得的石头数,到Alex选时要么选i要么选j,选的时候增加dp值,dp公式为\(dp[i][j] = max(piles[i] + dp[i+1][j], piles[j] + dp[i][j-1])\)。
对于Lee而言,他要使得dp值最小,同样要么选i要么选j,这时的选择会减少dp值,因为是Lee选择,会使得“Alex比Lee多获得的石头数”减少。dp公式为\(dp[i][j] = min(-piles[i] + dp[i+1][j], -piles[j] + dp[i][j-1])\)。
时间复杂度O(n2),空间复杂度O(n2)
做法3:事实上dp可以优化成一维数组,因为每次迭代是按dp矩阵的对角线迭代,从左方值(dp[i][j-1])或下方值(dp[i+1][j])选择。可以转为按行索引迭代(去掉列索引),dp的变化示意图如下,蓝色表示dp运算前的值,绿色代表运算dp需要使用的值,红色表示dp运算后得到的新值。从对角线开始(i等于j)不断迭代dp得到最后的红色就是最终结果。
时间复杂度O(n^2),空间复杂度O(n)

代码
做法1:
class Solution(object):
def stoneGame(self, piles):
"""
:type piles: List[int]
:rtype: bool
"""
return True
做法2:
class Solution(object):
def stoneGame(self, piles):
"""
:type piles: List[int]
:rtype: bool
"""
dp = [ [0 for _ in range(len(piles))] for _ in range(len(piles)) ]
for size in range(2,len(piles)+1):
for i in range(0,len(piles)-size+1):
j = i+size-1
if( size % 2 == 0):
dp[i][j] = max(piles[i] + dp[i+1][j], piles[j] + dp[i][j-1])
else:
dp[i][j] = min(-piles[i] + dp[i+1][j], -piles[j] + dp[i][j-1])
return dp[0][len(piles)-1] >= 0
做法3:
class Solution(object):
def stoneGame(self, piles):
"""
:type piles: List[int]
:rtype: bool
"""
dp = [0]* len(piles)
for size in range(2,len(piles)+1):
for i in range(0,len(piles)-size+1):
j = i+size-1
if( size % 2 == 0):
dp[i] = max(piles[i] + dp[i+1], piles[j] + dp[i])
else:
dp[i] = min(-piles[i] + dp[i+1], -piles[j] + dp[i])
return dp[0] >= 0
类似题目
877. Stone Game的更多相关文章
- 877. Stone Game - LeetCode
Question 877. Stone Game Solution 题目大意: 说有偶数个数字,alex和lee两个人比赛,每次轮流从第一个数字或最后一个数字中拿走一个(偶数个数字,所以他俩拿的数字个 ...
- [LeetCode] 877. Stone Game 石子游戏
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, ...
- LeetCode 877. Stone Game
原题链接在这里:https://leetcode.com/problems/stone-game/ 题目: Alex and Lee play a game with piles of stones. ...
- LC 877. Stone Game
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, ...
- leetcode 877. Stone Game 详解 -——动态规划
原博客地址 https://blog.csdn.net/androidchanhao/article/details/81271077 题目链接 https://leetcode.com/proble ...
- [LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, ...
- 【leetcode】877. Stone Game
题目如下: Alex and Lee play a game with piles of stones. There are an even number of piles arranged in ...
- 【LeetCode】877. Stone Game 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 双函数 单函数 + 记忆化递归 动态规划 日期 ...
- LeetCode contest-95[876,877,👁878]
876. Middle of the Linked List first submission # Definition for singly-linked list. # class ListNod ...
随机推荐
- 分页技巧__在项目中使用QueryHelper辅助对象实现分页效果
分页技巧__在项目中使用QueryHelper辅助对象实现分页效果 QueryHelper 用于辅助拼接HQL语句 addCondition("t.type=?", "精 ...
- ajax的轮询和长轮询
概念: 轮询(polling):客户端按规定时间定时像服务端发送ajax请求,服务器接到请求后马上返回响应信息并关闭连接. 概念总是枯燥的,只有代码方能解心头之快 前段代码:index.html: & ...
- zoj3696(泊松分布)
p(k)=(y^k) / (k!) * e^(-y) 其中的y就是平均值 k就是我们要求的大小. Alien's Organ Time Limit: 2 Seconds Memory Lim ...
- sessionStorage存储json对象
应用场景: 账单列表中A页面:点击其中的一列,ajax返回的数据在这一页 点击进入账单详情B页面: 因为在A页面已经做过ajax的请求了,所以希望把当前其中的一个数组对象传到B页面中,所以,就考虑到暂 ...
- Django学习笔记第三篇--关于响应返回
一.返回简单类型: #1.返回简单字符串 #from django.http import HttpResponse return HttpResponse("return string&q ...
- ios 开发环境,证书和授权文件
一.成员介绍1. Certification(证书)证书是对电脑开发资格的认证,每个开发者帐号有一套,分为两种:1) Developer Certification(开发证书)安装在电脑上 ...
- unicode转换中文
<!doctype html><html lang="en"> <head> <meta http-equiv="Refres ...
- Python--进阶处理4
#================第四章:迭代器和生成器=================== # 函数 itertools.islice()适用于在迭代器和生成器上做切片操作. import ite ...
- react 组件积累
material-ui material-table ant-design https://ant.design/docs/react/getting-started-cn 定义组件(注意,组件的名称 ...
- Mysql2索引
索引分类: 作用:优化查询,select查询有三种情况:缓存查询(不在mysql中进行数据查询),全表查询,索引扫描 Btree(btree b+tree b*tree) Rtree HASH Ful ...