LintCode "Coins in a Line III" !!
https://codesolutiony.wordpress.com/2015/05/24/lintcode-coins-in-a-line-iii/
A very juicy one! Deserve more consideration.
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;
// Step 1: Find Variables
// since it is 2-end a single dim i is not enough, so - i, j (left, right)
// and dp[i][j] is the max value Play1 can get in [i, j]
vector<vector<int>> dp(n, vector<int>(n));
// Step 2: Choice strategy
// Apparently in every step, we have 2 choices: pick left or right
// so dp[i][j] should depends on dp[i-1][j] and dp[i][j-1]
//
// Step 2.5 Game Thoery
// In each choice at step2, we always Play2 always made the optimal
// choice in last step
//
// Equation
// dp[i][j] = max(
// values[i] + sum[i+1][j] - dp[i+1][j],
// values[j] + sum[i][j-1] - dp[i][j-1]
// );
vector<int> presum(n);
partial_sum(values.begin(), values.end(), presum.begin());
for(int j = ; j < n; j ++)
for(int i = j; i >= ; i --)
{
if (i == j)
{
dp[i][j] = values[i];
}
else
{
int sumij = presum[j] - (i > ? presum[i - ] : );
dp[i][j] = sumij - min(dp[i+][j], dp[i][j-]);
}
}
return dp[][n-] > (presum.back() - dp[][n-]);
}
};
LintCode "Coins in a Line III" !!的更多相关文章
- LintCode: coins in a line I
有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 2, ...
- [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 一条线上的硬币
There are n coins in a line. Two players take turns to take one or two coins from right side until t ...
- Coins in a Line III
Description There are n coins in a line, and value of i-th coin is values[i]. Two players take turns ...
- 396. Coins in a Line III
刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...
- LintCode "Coins in a Line II" !
Nice one to learn: DP + Game Theoryhttps://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2 ...
- LintCode "Coins in a Line"
Recursion + Memorized Search(DP). And apparently, the code below can be iterative with only 3 vars - ...
- [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, ...
- 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个石子之后都是必胜,则当前必败 ...
随机推荐
- B3log部署文档
https://github.com/b3log/solo/wiki/standalone_mode 独立模式 只要已经安装好了 Java 环境,一个命令就能启动! 不依赖 MySQL 数据库,而是使 ...
- Matlab 2012a 下载和安装教程
迅雷下载地址 http://bbs.pinggu.org/thread-1426998-1-1.html(下载速度比较快) 1 Setup.exe 2. 不使用 internet 安装 then ...
- CentOS6 Squid代理服务器的安装与配置
代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息.Squid是一个缓存Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用户想要下载一 ...
- ZSDR101-跑成品MRP
*&---------------------------------------------------------------------**& Report ZSDR101*&a ...
- session处理超时的三种方式
1. 在web容器中设置(此处以tomcat为例) 在tomcat-5.0.28\conf\web.xml中设置,以下是tomcat 5.0中的默认配置: <!-- ========= ...
- 第三篇T语言热键启动方法
热键控件 热键控件的使用方法 第一步:选择热键添加到面板上. 第二步:热键控件属性选择热键键值. 第三步:添加热键事件功能. 热键模式的选择 1.普通模式 2.软件模式 3.硬件模式 脚本的基本操作 ...
- HTML DOM部分---document对象;
<style type="text/css"> #d3{ color:red} </style> </head> <body> &l ...
- jquery】常用的jquery获取表单对象的属性与值
[jquery]常用的jquery获取表单对象的属性与值 1.JQuery的概念 JQuery是一个JavaScript的类库,这个类库集合了很多功能方法,利用类库你可以用一些简单的代码实现一些复杂的 ...
- img元素底部有空白间距的问题
<div style="width:100px;height:100px"><img src="./1.jpg"></div> ...
- 释放Linux系统预留的硬盘空间【转】
http://www.cnblogs.com/ggjucheng/archive/2012/10/07/2714294.html 前言 大多数文件系统都会保留一部分空间留作紧急情况时用(比如硬盘空间满 ...