https://codesolutiony.wordpress.com/2015/05/24/lintcode-coins-in-a-line-iii/

A very juicy one! Deserve more consideration.

  1. class Solution {
  2. public:
  3. /**
  4. * @param values: a vector of integers
  5. * @return: a boolean which equals to true if the first player will win
  6. */
  7. bool firstWillWin(vector<int> &values) {
  8. int n = values.size();
  9.   if (n < ) return true;
  10.  
  11. // Step 1: Find Variables
  12. // since it is 2-end a single dim i is not enough, so - i, j (left, right)
  13. // and dp[i][j] is the max value Play1 can get in [i, j]
  14. vector<vector<int>> dp(n, vector<int>(n));
  15.  
  16. // Step 2: Choice strategy
  17. // Apparently in every step, we have 2 choices: pick left or right
  18. // so dp[i][j] should depends on dp[i-1][j] and dp[i][j-1]
  19. //
  20. // Step 2.5 Game Thoery
  21. // In each choice at step2, we always Play2 always made the optimal
  22. // choice in last step
  23. //
  24. // Equation
  25. // dp[i][j] = max(
  26. // values[i] + sum[i+1][j] - dp[i+1][j],
  27. // values[j] + sum[i][j-1] - dp[i][j-1]
  28. // );
  29.  
  30. vector<int> presum(n);
  31. partial_sum(values.begin(), values.end(), presum.begin());
  32.  
  33. for(int j = ; j < n; j ++)
  34. for(int i = j; i >= ; i --)
  35. {
  36. if (i == j)
  37. {
  38.   dp[i][j] = values[i];
  39. }
  40. else
  41. {
  42.   int sumij = presum[j] - (i > ? presum[i - ] : );
  43.   dp[i][j] = sumij - min(dp[i+][j], dp[i][j-]);
  44. }
  45. }
  46.  
  47. return dp[][n-] > (presum.back() - dp[][n-]);
  48. }
  49. };

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. mark资料-python编辑器的选择与安装

    1.pycharm 下载地址: 注册码: 注意事项: 2.ulipad参考虫师的地址 url: 3.eclipse+pydev

  2. 编写postgresql函数执行循环copy命令导入大数据

    CREATE OR REPLACE FUNCTION copyData() RETURNS boolean AS $BODY$ DECLARE i int; begin i :=1; FOR i IN ...

  3. Android中GC_EXTERNAL_ALLOC的含义

    GC_FOR_MALLOC means that the GC was triggered because there wasn't enough memory left on the heap to ...

  4. 开机流程与主引导分区(MBR)——鸟哥私房菜

    在前篇随笔中,已经谈到了CMOS与BIOS,CMOS是记录各项硬件参数(包括系统时间.设备的I/O地址.CPU的电压和频率等)且嵌入到主板上面的存储器,BIOS是一个写入到主板上的韧体(韧体是写入到硬 ...

  5. [luogu P2170] 选学霸(并查集+dp)

    题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...

  6. <老友记>学习笔记

    这是六个人的故事,从不服输而又有强烈控制欲的monica,未经世事的千金大小姐rachel,正直又专情的ross,幽默风趣的chandle,古怪迷人的phoebe,花心天真的joey——六个好友之间的 ...

  7. kuangbin_ShortPath M (POJ 1062)

    提出了一个错误的算法 以为能优化到只运行两次dij 然而我还是too naive 还是乖乖dij n 次吧... #include <iostream> #include <stri ...

  8. read the python code and predict the results --- from <Learn Python The Hard Way>

    import random from urllib import urlopen import sys WORD_URL = "http://learncodethehardway.org/ ...

  9. JSBinding / Run Samples

    This document shows you how to run JSBinding 2048 sample in Editor. First of course, create an empty ...

  10. GDB动态库搜索路径

    当GDB无法显示so动态库的信息或者显示信息有误时,通常是由于库搜索路径错误导致的,可使用set sysroot.set solib-absolute-prefix.set solib-search- ...