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"的更多相关文章

  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. LintCode "Coins in a Line III" !!

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

  5. LintCode "Coins in a Line II" !

    Nice one to learn: DP + Game Theoryhttps://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2 ...

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

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

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

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

随机推荐

  1. PAT (Basic Level) Practise:1005. 继续(3n+1)猜想

    [题目链接] 卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进 ...

  2. python, itertools模块

    通过itertools模块,可以用各种方式对数据进行循环操作 1, chain() from intertools import chain for i in chain([1,2,3], ('a', ...

  3. Vimium 快捷键记录

    , <c-e> : Scroll down k, <c-y> : Scroll up h : Scroll left l : Scroll right gg : Scroll ...

  4. href="#"与javascript:void(0)的区别

    共同点:都是一个空链接. 不同点:所以,#与javascript:void(0)的区别也很明显,#方法会跳转到页面的顶部,并且在页面URL后面会出现#,而javascript:void(0)方法不会, ...

  5. 2015GitWebRTC编译实录

    整体解决思路1 编译完成后2~3天,对之前编译的lib库进行测试.目前阶段至少保证真机测试是ok的,模拟器先放到一边.2015.06.24完成编译状况编译完成libjsoncpp编译完成libsyst ...

  6. 图像质量评价指标之Matlab实现

    在图像处理算法研究中,很多时候需要有客观评价指标来对算法的性能进行评价. 比如,在图像复原.图像滤波算法研究中,需要采用客观评价指标来定量的来测试算法恢复出的图像相对于参考图像的好坏程度. 本文介绍文 ...

  7. TextView所有属性

    android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web/email/phone/map/all) android: ...

  8. SpringMVC @Value取值(取properties属性文件的属性值)

    @Controller @RequestMapping("/reg") public class RegController extends BaseController { @V ...

  9. JVM 虚拟化

    http://www.infoq.com/cn/news/2015/05/java20-multitenant-jvm http://2016.qconshanghai.com/presentatio ...

  10. VS 2012 C#快捷键

    ctrl + J 重现智能提示 ctrl + L    删除一行ctrl + K ctrl + C 注释选中行ctrl +K ctrl +U    取消注释 ctrl +K ctrl +F    格式 ...