Nice one to learn: DP + Game Theory
https://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2/coinsInLine2.html

In game theory, we assume the other player always take optimal step too. Combining with DP, we put this assumption together with each DP selection.

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; // Backward DP: since dp[a] -> dp[b] where a > b
vector<long long> dp(n, );
// init
dp[n-] = values[n-]; // pick 1
dp[n-] = values[n-] + values[n-]; // pick 2 for(int i = n - ; i >= ; i --)
{
int dp2 = ,dp3 = , dp4 = ;
if(i < n - ) dp4 = dp[i + ];
if(i < n - ) dp3 = dp[i + ];
if(i < n - ) dp2 = dp[i + ]; // we pick min because the other player is smart too
int v1 = values[i] + min(dp2, dp3); // pick 1
int v2 = values[i] + values[i + ] + min(dp3, dp4); // pick 2
dp[i] = max(v1, v2);
} long long suma = accumulate(values.begin(), values.end(), );
return dp[] > (suma - dp[]);
}
};

LintCode "Coins in a Line II" !的更多相关文章

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

  2. LintCode: coins in a line I

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

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

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

  5. Lintcode395 Coins in a Line II solution 题解

    [题目描述] There are n coins with different value in a line. Two players take turns to take one or two c ...

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

  7. 395. Coins in a Line II

    最后更新 这个题做得也不好,dp[n]尝试写了几下,不太对. 应该是类似于gem theory的题. 当只有1个硬币剩下的时候直接拿走,不BB. 剩俩的时候也都拿了.. dp[n]表示剩下多少个硬币. ...

  8. LintCode "Coins in a Line III" !!

    https://codesolutiony.wordpress.com/2015/05/24/lintcode-coins-in-a-line-iii/ A very juicy one! Deser ...

  9. LintCode "Coins in a Line"

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

随机推荐

  1. 【avalon源码】scpCompile

    function noop() {} function scpCompile(array) { return Function.apply(noop, array) } // var fn = new ...

  2. Debian 安装下载工具软件

    Debian 安装下载工具软件 1.下载BT种子Torrent文件 Linux下载种子文件肯定不能使用迅雷了,推荐一款叫做qBittorrent的P2P下载软件,目前在Ubuntu中使用很广泛,同样D ...

  3. OpenFlow Switch学习笔记(五)——Group Table、Meter Table及Counters

    本文主要详述OpenFlow Switch的另外两个主要组件——Group Table和Meter Table,它们在整个OpenFlow Swtich Processing中也起到了重要作用. 1. ...

  4. Highcharts入门+Highcharts基础教程,【非常值得学习的资料】

    http://www.hcharts.cn/docs/index.php?doc=index Highcharts入门章节目录 Highcharts简介 Highcharts下载与使用 Highcha ...

  5. UVa 11246 - K-Multiple Free set

    题意大意: 一个{1..n}的集合,求一个子集合,使得元素个数最多,并且不存在有两个元素x * k = y,求出最多的元素个数是多少. 分析: 先要删除k倍的,删除为{k, 2k, 3k, 4k, 5 ...

  6. 264. Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  7. dll清理

    結束了閉關,又得與人類交流了,只好裝QQ 印象中企鵝手腳一向是BAT中最乾淨的-結果還是裝了不少樂色,一併挖出來除之   C:\program files\common files\tencent\q ...

  8. MACOS,LINUX,IOS上可用的毫秒级精度时间获取

    二话不说,先上代码 void outputCurrentTime(uint32_t seq,const char* type){ struct timeval t_curr; gettimeofday ...

  9. 使用isInEditMode解决可视化编辑器无法识别自定义控件的问题

    如果在自定义控件的构造函数或者其他绘制相关地方使用系统依赖的代码, 会导致可视化编辑器无法报错并提示:Use View.isInEditMode() in your custom views to s ...

  10. leetcode 119 Pascal's Triangle II ----- java

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...