LintCode "Coins in a Line III" !!
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" !!的更多相关文章
- LintCode: coins in a line I
有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 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 ...
- [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 ...
- 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 ...
- 396. Coins in a Line III
刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...
- LintCode "Coins in a Line II" !
Nice one to learn: DP + Game Theoryhttps://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2 ...
- LintCode "Coins in a Line"
Recursion + Memorized Search(DP). And apparently, the code below can be iterative with only 3 vars - ...
- [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, ...
- 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个石子之后都是必胜,则当前必败 ...
随机推荐
- mark资料-python编辑器的选择与安装
1.pycharm 下载地址: 注册码: 注意事项: 2.ulipad参考虫师的地址 url: 3.eclipse+pydev
- 编写postgresql函数执行循环copy命令导入大数据
CREATE OR REPLACE FUNCTION copyData() RETURNS boolean AS $BODY$ DECLARE i int; begin i :=1; FOR i IN ...
- Android中GC_EXTERNAL_ALLOC的含义
GC_FOR_MALLOC means that the GC was triggered because there wasn't enough memory left on the heap to ...
- 开机流程与主引导分区(MBR)——鸟哥私房菜
在前篇随笔中,已经谈到了CMOS与BIOS,CMOS是记录各项硬件参数(包括系统时间.设备的I/O地址.CPU的电压和频率等)且嵌入到主板上面的存储器,BIOS是一个写入到主板上的韧体(韧体是写入到硬 ...
- [luogu P2170] 选学霸(并查集+dp)
题目传送门:https://www.luogu.org/problem/show?pid=2170 题目描述 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果实力相当的人中,一部分被选上,另一 ...
- <老友记>学习笔记
这是六个人的故事,从不服输而又有强烈控制欲的monica,未经世事的千金大小姐rachel,正直又专情的ross,幽默风趣的chandle,古怪迷人的phoebe,花心天真的joey——六个好友之间的 ...
- kuangbin_ShortPath M (POJ 1062)
提出了一个错误的算法 以为能优化到只运行两次dij 然而我还是too naive 还是乖乖dij n 次吧... #include <iostream> #include <stri ...
- 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/ ...
- JSBinding / Run Samples
This document shows you how to run JSBinding 2048 sample in Editor. First of course, create an empty ...
- GDB动态库搜索路径
当GDB无法显示so动态库的信息或者显示信息有误时,通常是由于库搜索路径错误导致的,可使用set sysroot.set solib-absolute-prefix.set solib-search- ...