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. 用Xshell访问 虚拟机里的kali

    1.vim /etc/ssh/sshd_config 2.PermitRootLogin without-password复制 3.把一个PermitRootLogin without-passwor ...

  2. WeCenter程序安装

    WeCenter程序安装时需要GD库和freetype的支持,以下是安装方法 GD库的安装:我们可以直接使用yum命令来安装,自动解决依赖关系及安装GD库相关的包. [root@localhost ~ ...

  3. Winform 关于委托与Invoke和Begin Invoke的使用

    这方面的文章已经写得很详细了,特地摘引两篇文章 http://www.cnblogs.com/c2303191/articles/826571.html http://www.cnblogs.com/ ...

  4. Vimium 快捷键记录

    , <c-e> : Scroll down k, <c-y> : Scroll up h : Scroll left l : Scroll right gg : Scroll ...

  5. 70. Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  6. leetcode 120 Triangle ----- java

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. JavaWeb学习记录(一)——response响应头之缓存设置与下载功能的实现

    一.HTTP中常用响应头 Location: http://www.it315.org/index.jsp Server:apache tomcat Content-Encoding: gzip Co ...

  8. Codeforces Round #142 (Div. 2)

    A. Dragons 按\(x\)排序. B. T-primes \(x\)是平方数,且根\(\sqrt{x}\)是个质数. C. Shifts 枚举列的位置,对于每行来说,最多只会涉及4个列. D. ...

  9. 固定虚拟机的IP

  10. 织梦DedeCMS"当前位置"去除最后一个 > 符号的方法

    首先找到根目录下面的include 目录,然后找到 typelink.class.php 文件, 再查找到 GetPositionLink 方法 下面的 return $this->valueP ...