问题

有偶数堆石头(数组长度为偶数),每堆石头有一些石头(数组元素为正),石头的总数是奇数。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

类似题目

486. Predict the Winner

877. Stone Game的更多相关文章

  1. 877. Stone Game - LeetCode

    Question 877. Stone Game Solution 题目大意: 说有偶数个数字,alex和lee两个人比赛,每次轮流从第一个数字或最后一个数字中拿走一个(偶数个数字,所以他俩拿的数字个 ...

  2. [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, ...

  3. LeetCode 877. Stone Game

    原题链接在这里:https://leetcode.com/problems/stone-game/ 题目: Alex and Lee play a game with piles of stones. ...

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

  5. leetcode 877. Stone Game 详解 -——动态规划

    原博客地址 https://blog.csdn.net/androidchanhao/article/details/81271077 题目链接 https://leetcode.com/proble ...

  6. [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, ...

  7. 【leetcode】877. Stone Game

    题目如下: Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in ...

  8. 【LeetCode】877. Stone Game 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学 双函数 单函数 + 记忆化递归 动态规划 日期 ...

  9. LeetCode contest-95[876,877,👁878]

    876. Middle of the Linked List first submission # Definition for singly-linked list. # class ListNod ...

随机推荐

  1. jquery后加Dom绑定事件

    $('#musicCategoryListContainer').on('click', '.musicCategoryItem', function () { $(this).siblings(). ...

  2. iOS开发之-- textview 光标起始位置偏移

    使用textview的时候,会发生光标偏移的情况,其实是因为iOS7里导航栏,状态栏等有个边缘延伸的效果在. 把边缘延伸关掉就好了.代码如下 //取消iOS7的边缘延伸效果(例如导航栏,状态栏等等) ...

  3. ArcGIS -- ArcSDE Lock request conflicts with an established lock

    1.查询下列表是否有数据 select * from sde.state_locks; select * from sde.object_locks; select * from sde.layer_ ...

  4. PMP 质量管理7张图 很形象

    PMP 质量管理 中的因果图.控制图.流程图.核查表.直方图.帕累托图.散点图

  5. 【BZOJ1486】[HNOI2009]最小圈 分数规划

    [BZOJ1486][HNOI2009]最小圈 Description Input Output Sample Input 4 5 1 2 5 2 3 5 3 1 5 2 4 3 4 1 3 Samp ...

  6. SPS读书笔记1——均值比较(T检验,方差检验,非参数检验汇总)

    均值比较.单样本T检验(One-sample Test))目的:检验单个变量的均值与给定的某个常数是否一致.)判断标准:p<0.05;t>1.98即认为是有显著差异的..独立样本T检验(I ...

  7. Docker的基本使用(部署python项目)

    今天开始利用docker来部署项目,当然,首先,需要安装好Docker,这个在我的上篇中写了 一.准备项目 我写的是一个爬取某ppt网站的代码,就一个ppt1.py是爬虫,然后,ppts是存放下载的p ...

  8. Maven 构建Spring-Boot工程报错

    Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:repackage ...

  9. 部分 II. 保护web篇

    转载:http://www.mossle.com/docs/auth/html/pt02-web.html 部分 II. 保护web篇   2012-12-5 23:42:36 org.springf ...

  10. Centos7.2yum安装时候出现db5错误的解决办法

    Centos7.2使用yum安装软件是出现如此错误提示 解决办法 删除 /var/lib/rpm文件夹下面所有以__db开头的文件