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. 循环列表的Java实现,解决约瑟夫环问题

    import java.util.Scanner; /** * 循环列表的Java实现,解决约瑟夫环问题 * * @author LIU * */ public class LinkedList { ...

  2. Think Python - Chapter 16 - Classes and functions

    16.1 TimeAs another example of a user-defined type, we’ll define a class called Time that records th ...

  3. java 中的几种 "通用方法“

    前言 Java中,除了基本的数值类型,其他所有数据类型(包括数组)都是对象. 而Object这个类是所有类的超类,它提供的方法,自然能够使用于它的所有子类(所有非基本数值类型). 本文介绍了Objec ...

  4. (实用篇)PHPExcel读取Excel文件的实现代码

    用PHPExcel读取Excel 2007 或者Excel2003文件,需要的朋友,可以参考下. 涉及知识点:  php对excel文件进行循环读取 php对字符进行ascii编码转化,将字符转为十进 ...

  5. UVa 12558 - Egyptian Fractions (HARD version)

    题目大意: 给出一个真分数,把它分解成最少的埃及分数的和.同时给出了k个数,不能作为分母出现,要求解的最小的分数的分母尽量大. 分析: 迭代加深搜索,求埃及分数的基础上,加上禁用限制就可以了.具体可以 ...

  6. iOS学习笔记---oc语言第七天

    类的扩展 NSDate是Cocoa中用于处理日期和时间的基础类,封装了某一给定的时刻,具体的日期 时间和时区 使用+date方法获取当前日期和时间 NSDate *date = [NSDate dat ...

  7. 获取元素高度及定位js

    <script type="text/javascript">                                   $(window).scroll(f ...

  8. Spring AOP 实现写事件日志功能

    什么是AOP?AOP使用场景?AOP相关概念?Spring AOP组件?如何使用Spring AOP?等等这些问题请参考博文:Spring AOP 实现原理 下面重点介绍如何写事件日志功能,把日志保存 ...

  9. C++ Primer : 第二章:变量和基本类型(1)

    变量和基本类型之第一篇:基本内置类型和变量 一. (1) C++定义了一套包括算数类型和空类型,这些类型有:布尔类型bool,字符类型char,宽字符类型wchar_t,Unicode字符char16 ...

  10. SimPholders2 模拟器 App 文件路径查看工具

    SimPholder2.app 官网下载地址:http://www.simpholders.com ​当使用 Xcode beta 版本切换到 Xcode 正式版本时,点击 SimPholders2. ...