LintCode "Coins in a Line II" !
Nice one to learn: DP + Game Theory
https://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2/coinsInLine2.html
In game theory, we assume the other player always take optimal step too. Combining with DP, we put this assumption together with each DP selection.
class Solution {
public:
/**
* @param values: a vector of integers
* @return: a boolean which equals to true if the first player will win
*/
bool firstWillWin(vector<int> &values) {
int n = values.size();
if(n<) return true; // Backward DP: since dp[a] -> dp[b] where a > b
vector<long long> dp(n, );
// init
dp[n-] = values[n-]; // pick 1
dp[n-] = values[n-] + values[n-]; // pick 2 for(int i = n - ; i >= ; i --)
{
int dp2 = ,dp3 = , dp4 = ;
if(i < n - ) dp4 = dp[i + ];
if(i < n - ) dp3 = dp[i + ];
if(i < n - ) dp2 = dp[i + ]; // we pick min because the other player is smart too
int v1 = values[i] + min(dp2, dp3); // pick 1
int v2 = values[i] + values[i + ] + min(dp3, dp4); // pick 2
dp[i] = max(v1, v2);
} long long suma = accumulate(values.begin(), values.end(), );
return dp[] > (suma - dp[]);
}
};
LintCode "Coins in a Line II" !的更多相关文章
- [LintCode] Coins in a Line II 一条线上的硬币之二
There are n coins with different value in a line. Two players take turns to take one or two coins fr ...
- LintCode: coins in a line I
有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 2, ...
- 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] Coins in a Line 一条线上的硬币
There are n coins in a line. Two players take turns to take one or two coins from right side until t ...
- Lintcode395 Coins in a Line II solution 题解
[题目描述] There are n coins with different value in a line. Two players take turns to take one or two c ...
- Coins in a Line II
There are n coins with different value in a line. Two players take turns to take one or two coins fr ...
- 395. Coins in a Line II
最后更新 这个题做得也不好,dp[n]尝试写了几下,不太对. 应该是类似于gem theory的题. 当只有1个硬币剩下的时候直接拿走,不BB. 剩俩的时候也都拿了.. dp[n]表示剩下多少个硬币. ...
- LintCode "Coins in a Line III" !!
https://codesolutiony.wordpress.com/2015/05/24/lintcode-coins-in-a-line-iii/ A very juicy one! Deser ...
- LintCode "Coins in a Line"
Recursion + Memorized Search(DP). And apparently, the code below can be iterative with only 3 vars - ...
随机推荐
- 基于Open vSwitch的OpenFlow实践
Open vSwitch(下面简称为 OVS)是由 Nicira Networks 主导的,运行在虚拟化平台(例如 KVM,Xen)上的虚拟交换机.在虚拟化平台上,OVS 可以为动态变化的端点提供 2 ...
- iOS学习笔记---oc语言第九天
初级内存管理 iOS应用程序出现crash(闪退),90%以上是内存问题////其他:数组越界,方法只声明没实现 内存问题体现在两个方面:内存溢出\野指针异常 内存溢出:程序运行超出内存上限 野指针异 ...
- ion-tap选项卡及路由结合ion-tap
ion-tabs简介 <!DOCTYPE html> <html ng-app="ionic"> <head> <meta name=&q ...
- ZOJ 1047 Image Perimeters
原题链接 题目大意:鼠标点击一块,求与之联通的所有区域的边长之和. 解法:广度优先搜索.从选中的这个点开始,往周围8个点依次搜索,访问过的点做上标记.如果该点上下左右的一个或多个方向没有相邻的点,边长 ...
- 单元测试--四则运算2程序(c++)
源代码: //2016 3.6 Cheng Qiqin //四则运算改进 #include <iostream> #include<ctime> #include<cst ...
- 1-3-2 Windows应用程序常用消息
主要内容:介绍Windows编程中常用的消息 1.WM_LBUTTONDOWN产生单击鼠标左键的消息 lParam: 低字节包含当前光标的X坐标值 X = LOWORD(lParam); 高字节包含当 ...
- 穿越泥地(mud) (BFS)
问题 C: 穿越泥地(mud) 时间限制: 1 Sec 内存限制: 128 MB提交: 16 解决: 10[提交][状态][讨论版] 题目描述 清早6:00,FJ就离开了他的屋子,开始了他的例行工 ...
- Python Web.py
安装Web.py root@bt:~# sudo pip install web.py Downloading/unpacking web.py Downloading web.py-0.37.tar ...
- (转) 解决ssh的"Write failed: Broken pipe"问题
解决ssh的"Write failed: Broken pipe"问题 问题场景 服务器环境:阿里云 Linux CentOS 主机 客户端:Mac OSX Terminal ...
- rac 11g_生产库日志组损坏处理
原创作品,出自 "深蓝的blog" 博客,转载时请务必注明出处,否则有权追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/ar ...