Alex and Lee play a game with piles of stones.  There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].

The objective of the game is to end with the most stones.  The total number of stones is odd, so there are no ties.

Alex and Lee take turns, with Alex starting first.  Each turn, a player takes the entire pile of stones from either the beginning or the end of the row.  This continues until there are no more piles left, at which point the person with the most stones wins.

Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.

Example 1:

Input: [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.

Note:

  1. 2 <= piles.length <= 500
  2. piles.length is even.
  3. 1 <= piles[i] <= 500
  4. sum(piles) is odd.

这个题目思路跟[LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈很像, 只不过这里是利用 区间Dynamic Programing的方法,所以只用一维的dp已经不够了, 另外初始化的时候我们不直接用for loop, 而是用类似于dfs recursive的方法去将初始化放在helper fuction里面. 另外得到的

动态方程式为  A[i][j] = max( piles[i] + min(A[i+1][j-1] + A[i+2][j]) , piles[j] + min(A[i+1][j-1], A[i][j-2]) )

init;

A[i][i] = piles[i]

A[i][i+1] = max(piles[i], piles[i+1])

1. Constraints

1) size [2,500], even number

2) element [1,50], integer

3) sum(piles) is odd, no ties

2. Ideas

Dynamic Programming   ,     T: O(n^2)     S; O(n^2)

3. Code

 class Solution:
def stoneGame(self, piles):
n = len(piles)
dp, flag = [[0]*n for _ in range(n)], [[0]*n for _ in range(n)]
def helper(left, right):
if flag[left][right]:
return dp[left][right]
if left == right:
dp[left][right] = piles[left]
elif left + 1 = right:
dp[left][right] = max(piles[left], piles[right])
elif left < right: # left > right, init 0
value_l = piles[left] + min(helper(left+2, right), helper(left + 1, right -1))
value_r = piles[right] + min(helper(left+1, right-1), helper(left, right - 2))
dp[left][right] = max(value_l, value_r)
flag[left][right] = 1
return dp[left][right]
return helper(0, n-1) > sum(piles)//2

4. Test cases

[5,3,4,5]
 

[LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈的更多相关文章

  1. [LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  2. [LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈

    Description There are n coins with different value in a line. Two players take turns to take one or ...

  3. [LeetCode] questions conclusion_ Dynamic Programming

    Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...

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

  5. lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II

    变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...

  6. [LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈

    Description There are n coins in a line. Two players take turns to take one or two coins from right ...

  7. 396. Coins in a Line III

    刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...

  8. LeetCode 877. Stone Game

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

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

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

随机推荐

  1. css笔记 - 张鑫旭css课程笔记之 absolute 篇

    absolute地址 absolute绝对定位 绝对定位与浮动鲜为人知的兄弟关系 即是说,absolute后,元素和浮动元素的特性差不多,只不过absolute脱离文档流,元素飘在天上,float还在 ...

  2. css笔记 - 张鑫旭css课程笔记之 margin 篇

    margin - 人若犯我,我必犯人! [margin地址](https://www.imooc.com/learn/680) 一.margin与容器尺寸的关系 relative可定位,但是不改变容器 ...

  3. ubuntu 用aptitude代替apt-get处理依赖性问题

    aptitude 与 apt-get 一样,是 Debian 及其衍生系统中功能极其强大的包管理工具.与 apt-get 不同的是,aptitude 在处理依赖问题上更佳一些.举例来说,aptitud ...

  4. source.android.google && developer.android.google

    https://source.android.google.cn/ https://developer.android.google.cn/ https://source.android.com/co ...

  5. JVM工具jcmd实践

    暂时参考以下链接,后续补充自己的实践. https://www.jianshu.com/p/388e35d8a09b

  6. React 属性和状态的一些总结

    一.属性 1.第一种使用方法:键值对 <ClaaNameA name = “Tom” /> <ClaaNameA name = {Tom} /> <ClaaNameA n ...

  7. 2-sat入门(tarjan)hdu(3062)

    hdu3062 Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  8. Python 基础知识(二)

    一.基础数据类型 1.数字int 数字主要是用于计算用的,使用方法并不是很多,就记住一种就可以: #bit_length() 当十进制用二进制表示时,最少使用的位数 # -*- coding:UTF- ...

  9. 1.tTensorboard

    Windows下坑太多...... 在启动TensorBoard的过程,还是遇到了一些问题.接下来简单的总结一下我遇到的坑.        1.我没找不到log文件?!              答: ...

  10. Solr学习笔记之5、Component(组件)与Handler(处理器)学习

    Solr学习笔记之5.Component(组件)与Handler(处理器)学习 一.搜索篇 拼写检查(spellCheck) 作用:用来检查用户输入的检索内容是否存在,如果不存在则给它提示出相近或相似 ...