[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, 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:
2 <= piles.length <= 500
piles.length
is even.1 <= piles[i] <= 500
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, 博弈的更多相关文章
- [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 ...
- [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 ...
- [LeetCode] questions conclusion_ Dynamic Programming
Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...
- [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, ...
- 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个石子之后都是必胜,则当前必败 ...
- [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 ...
- 396. Coins in a Line III
刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...
- LeetCode 877. Stone Game
原题链接在这里:https://leetcode.com/problems/stone-game/ 题目: Alex and Lee play a game with piles of stones. ...
- leetcode 877. Stone Game 详解 -——动态规划
原博客地址 https://blog.csdn.net/androidchanhao/article/details/81271077 题目链接 https://leetcode.com/proble ...
随机推荐
- JavaScript怎样学
嘿,我最近接到一个 Web 项目,不过老实说,我这两年没怎么接触 Web 编程,听说 Web 技术已经发生了一些变化.听说你是这里对新技术最了解的 Web 开发工程师? 准确地说,我是一名「前端工程师 ...
- 总结一下最近用到的技术(2)--JsonSchema和JsonSchemaValidator
我们最早接触xml的时候会使用一个dtd文件去定义xml里可以有哪些元素和属性等,后来发展到xml schama(是一个xsd文件,在dtd的基础上提供了命名空间等更强大的功能) 现在,RESTful ...
- ThreadLocal的实现和使用场景
ThreadLocal 内部实现.应用场景和内存泄漏 深入理解线程局部变量:ThreadLocal <Java源码分析>:ThreadLocal /ThreadLocalMap Threa ...
- Unity3D笔记 英保通六 角色控制器
一.角色控制器 U3D有两种角色控制方式:Rigidbody刚体.角色控制器组件(胶囊体组件) 面试的题目中经常会遇到这个问题: CharacterController和Rigidbody的区别? 这 ...
- layer.load()加载层如何加入文字描述
https://fly.layui.com/jie/3586/ https://www.layui.com/doc/modules/layer.html#layer.load //loading层va ...
- C#控制台窗口居中显示(转)
private struct RECT { public int left, top, right, bottom; } [DllImport("kernel32.dll", Se ...
- TOP100summit:【分享实录】爆炸式增长的斗鱼架构平台的演进
本篇文章内容来自2016年TOP100summit斗鱼数据平台部总监吴瑞城的案例分享. 编辑:Cynthia 吴瑞诚:斗鱼数据平台部总监 曾先后就职于淘宝.一号店. 从0到1搭建公司大数据平台.平台规 ...
- 求全排列Permutation
是在教材(<计算机算法设计与分析(第4版)>王晓东 编著)上看见的关于求全排列的算法: 我们可以看一下书上怎么写的: #include<bits/stdc++.h> using ...
- pyAudio介绍
概要 pyaudio有这么几个功能: - 提取特征 - 训练并且使用分类器 - 语音分割功能 - 内容关系可视化 python实现,好处有这么几个 - 适合做计算分析类型操作(编码少,效率不低) - ...
- 静默方式安装10g数据库软件+升级patch+手工建库
通常我们安装Oracle数据库软件,都是用OUI图形界面来完成的,但有些Unix/Linux系统中并未安装图形系统,也就无法使用图形界面来安装Oracle的产品了,对于这种场景,就只能采用静默方式来安 ...