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 - ...
随机推荐
- Python编程小记
发现这种结构很实用: while True: expression .... if condition: expression break 好吧,我承认我是菜鸟-
- ZOJ 1243 URLs
/*In the early nineties, the World Wide Web (WWW) was invented. Nowadays, most people think that the ...
- 不容易系列之(3)—— LELE的RPG难题
有排成一行的n个方格,用红(Red).粉(Pink).绿(Green)三色涂每个格子,每格涂一色,要求任何相邻的方格不能同色,且首尾两格也不同色.求全部的满足要求的涂法. 思路:运用递归算法. a[1 ...
- Java基础相关
对老师上课内容进行总结: 1.新建一个Java项目,并命名为HelloWorld 然后再新建类,并命名为HelloWorld,注意红色画圈部分 若勾选,则新建类开头为(“//后的内容为老师所讲批注”) ...
- 由登录服务器时ulimit配置报错,也谈下ulimit配置
最近在登录开发机时,有报错如下: -bash: cannot modify limit: Operation not permitted 一定是哪个地方有ulimit设置,想想看,用户登录或用户su命 ...
- poj1502 最短路
题意:有n个处理器,给出n*n的邻接矩阵的一半,表示相互之间传输信息的花费(另一半与给出的一半对称,即双向都可传输),x表示不能传输,问从第一个处理器传输到所有处理器的最小花费总和是多少. 就是跑一遍 ...
- 利用mybatis_generator自动生成Dao、Model、Mapping相关文件
技术交流群:233513714 http://blog.csdn.net/wyc_cs/article/details/9023117 http://www.cnblogs.com/smileberr ...
- CUDA学习笔记(二)【转】
来源:http://luofl1992.is-programmer.com/posts/38847.html 编程语言的特点是要实践,实践多了才有经验.很多东西书本上讲得不慎清楚,不妨自己用代码实现一 ...
- DNS-解析、劫持、污染
DNS( Domain Name System)是“域名系统”的英文缩写,是一种组织成域层次结构的计算机和网络服务命名系统,它用于TCP/IP网络,它所提供的服务是用来将主机名和域名转换为IP地址的工 ...
- Js文本溢出自动添加省略号ellipsis
原文: ellipsis: function(value, len, word) { //判断value有没有超过指定长度 if (value && v ...