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" !!的更多相关文章

  1. LintCode: coins in a line I

    有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 2, ...

  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 ...

  3. [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 ...

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

  5. 396. Coins in a Line III

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

  6. LintCode "Coins in a Line II" !

    Nice one to learn: DP + Game Theoryhttps://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2 ...

  7. LintCode "Coins in a Line"

    Recursion + Memorized Search(DP). And apparently, the code below can be iterative with only 3 vars - ...

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

  9. 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个石子之后都是必胜,则当前必败 ...

随机推荐

  1. linux 网络联接

    安装完进入系统以后需要配置ip 方法一:图形界面直接配置,在system下的prefrecens下面的network下面设置 方法二: 在安装虚拟机的时候选择“bridge networking ”网 ...

  2. ci(转)

    1  从代码管理器签出源文件 2  修改代码 3  编译代码 4  遇到错误,转到2继续修改直到达到预期 5  运行单元测试,期望所有的测试绿色(通过) 6  单元测试出错,转入2 7  重构代码,按 ...

  3. 遇到double 数目过大,转String变成科学计数法

    问题: java中,当double数目过大,转出String时,变成了科学记数法的表示. 总结: 1.项目的存储用的是mysql,mysql的类型和java类型之间存在映射关系,以前关注不多.现在总结 ...

  4. vmware上的Linux获取uuid

    在挂载asm硬盘时需要硬盘的UUID 虚拟机配置中需要增加对UUID的支持. 在配置文件vmx文件中增加如下内容 disk.locking="FALSE" disk.EnableU ...

  5. Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.

    svn更新代码后,打开xcode工程文件,会出现  xxx..xcodeproj  cannot be opened because the project file cannot be parsed ...

  6. textarea元素在加上runat="server"后运行报错解决

    当出现这个报错的时候,在后台引用相应的命名空间,为 using System.Web.UI; using System.Web.UI.HtmlControls;using System.Web.UI. ...

  7. 装载:关于拉格朗日乘子法与KKT条件

    作者:@wzyer 拉格朗日乘子法无疑是最优化理论中最重要的一个方法.但是现在网上并没有很好的完整介绍整个方法的文章.我这里尝试详细介绍一下这方面的有关问题,插入自己的一些理解,希望能够对大家有帮助. ...

  8. Codeforces Round #367 (Div. 2) A B C 暴力 二分 dp(字符串的反转)

    A. Beru-taxi time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. Matlab神经网络工具箱学习之二

    螃蟹的分类 这个例子的目的是根据螃蟹的品种.背壳的长宽等等属性来判断螃蟹的性别,雄性还是雌性. 训练数据一共有六个属性: species, frontallip, rearwidth, length, ...

  10. Radiobutton编辑

    package com.example.yuekao3; import java.util.ArrayList;import java.util.List; import com.baidu.farm ...