LintCode "Coins in a Line"
Recursion + Memorized Search(DP). And apparently, the code below can be iterative with only 3 vars - DP.
class Solution {
unordered_map<int, bool> cache;
public:
bool go(int n)
{
if (n <=) return false;
if (n < ) return true; if(cache.find(n) == cache.end())
{
cache[n] = !go(n - ) || !go(n - );
}
return cache[n];
}
/**
* @param n: an integer
* @return: a boolean which equals to true if the first player will win
*/
bool firstWillWin(int n) {
return go(n);
}
};
LintCode "Coins in a Line"的更多相关文章
- 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 ...
- LintCode "Coins in a Line III" !!
https://codesolutiony.wordpress.com/2015/05/24/lintcode-coins-in-a-line-iii/ A very juicy one! Deser ...
- LintCode "Coins in a Line II" !
Nice one to learn: DP + Game Theoryhttps://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2 ...
- [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个石子之后都是必胜,则当前必败 ...
- Lintcode394 Coins in a Line solution 题解
[题目描述] There are n coins in a line. Two players take turns to take one or two coins from right side ...
- LeetCode 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 ...
随机推荐
- HTTP 简介
HTTP 简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传 ...
- C++ 中的C_str()函数用法
语法: const char *c_str(); c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过 ...
- Brainstorm in one sentence
[1]佚名|台湾学生占领立法院 人會長大三次 第一次是發現世界不是為自己而轉的時候. 第二次是在發現即使再怎麼努力,終究還是有些事令人無能為力的時候. 第三次是在,明知道有些事可能會無能為力,但還是會 ...
- Matlab位运算笔记
本文为转载其他地方的文章; MATLAB函数 1.matlab函数bitset 设置数的某一位二进制位为1. <Simulink与信号处理> 使用方法 C = bitset(A,bit) ...
- 推荐一些android开发学习的资料
网址: 1:http://v.youku.com/v_show/id_XMTgwMTQ1MTgw.html 2:http://mars.apkbus.com/ 3:http://wenku.baidu ...
- leetcode 100 Same Tree ----- java
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 3-1 rpm包命名规则
1.RPM包的来源 <1>RPM包在系统光盘中 ---------------------------------------------------------------------- ...
- URAL 1930 Ivan's Car(BFS)
Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are d ...
- phpwind将服务器数据同步到本地之后网站不显示或者排版错误
在将phpwind的数据同步到本地服务器之后 如果访问本地服务器的首页不能显示的话 首先要查看global.php文件中的D_P变量,官方默认 的此变量应该指向和R_P变量是同一个文件夹即网站的根目录 ...
- Understanding Delegates in C#
要学东西,还是得看第一手资料,看二.三手资料难免误会... http://www.codeproject.com/Articles/11657/Understanding-Delegates-in-C