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. Xen虚拟机磁盘镜像模板制作(四)—CentOS 7

    在<Xen虚拟机磁盘镜像模板制作(三)—CentOS 7>一文中,我们已经成功制作出了 CentOS7 磁盘镜像.下面我们说明下如何通过它来生成目标虚拟机,同时测试下之前制作好的虚拟机磁盘 ...

  2. win live

    ok, this is my first live blog; just for test. r U ok fmkf vdsvsvv svsvsvsv try{ }

  3. Standard C++ Programming: Virtual Functions and Inlining

    原文链接:http://www.drdobbs.com/cpp/standard-c-programming-virtual-functions/184403747 By Josée Lajoie a ...

  4. git不能提交jar的设置

      项目目录下 文件:.gitignore ,里面设置: *.class # Package Files # *.jar *.war *.ear 删除*.jar

  5. php 函数积累

    array_slice()<?php $a=array("red","green","blue","yellow" ...

  6. FTP方式获取文件步骤

    1.在浏览器地址行输入FTP连接地址 2.进入FTP端之后,点击“檢視”-“在windows資源總覽中開啟FTP站臺”,這樣就可以打開FTP端的文件了

  7. Windows 10触摸板手势

    高級使用者試用 Windows 10 筆記本電腦的觸控板上的這些手勢: •選擇一項: 在觸控板上點擊. •滾動: 將兩根手指放在觸控板上,然後以水準或垂直方向滑動. •放大或縮小: 將兩根手指放在觸控 ...

  8. CSS 阴影怎么写?

    只有CSS3才zh支持阴影效果,ke可以用如下写法:.shadow {-webkit-box-shadow:1px 1px 3px #292929;-moz-box-shadow:1px 1px 3p ...

  9. poj2492 带权并查集

    题意:研究一种生物,有n个生物个体,发现有一些之间进行了交配,给出了这些关系,问是否有同性恋的bug出现. 用0\1表示某元素和其祖先元素的性别关系,0 为相同,1 为不同,用 mod2 实现累计处理 ...

  10. <初级程序员> git 的初级使用

    作为程序员,Git 是一个很好的代码管理工具.Git 是一个版本控制系统,主要的作用就是记录代码的修改过程,有效的追踪文件的变化.当代码出现错误的时候可以很容易的恢复到之前的状态,不管对于个人开发还是 ...