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. How to adjust OOM score for a process?

    转载自http://www.dbasquare.com/kb/how-to-adjust-oom-score-for-a-process/ How to adjust OOM score for a ...

  2. C语言 负数取余的原理

    负数求余数运算是一个数学问题: 任何一个整数n都可以表示成 n=k*q+r 其中0<=|r|<|q| 这里的r就是n除以q的余数,即 r==n%q 例如: -9=(-2)*4+(-1) 则 ...

  3. Sprint第二个冲刺(第六天)

    一.Sprint 计划会议: 因为这两天课程较多的原因,表面上进度可能可能没有太大的变化,其实组员们都有完善之前做的功能,正在做的功能也在抓紧时间完成.俗话说得好,慢工出细活 ,不能因为赶进度就随便做 ...

  4. lable自动适配大小

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  5. abbyy cup a

    link: http://codeforces.com/contest/331/problem/A2 /* ID: zypz4571 LANG: C++ TASK: abby_a.cpp */ #in ...

  6. Codeforces Round #126 (Div. 2)

    A. Cinema 假设当前要的位置为\((x, y)\),如果枚举答案的横坐标,那么每次找离\(y\)最近的纵坐标. 如果占用了位置\((x,y)\),需要要更新第\(x\)行的信息,而占用位置\( ...

  7. UVALive-7303 Aquarium (最小生成树)

    题目大意:在nxm的方格中,每一个1x1的小方格中都有一堵沿对角线的墙,并且每堵墙都有一个坚固程度,这些墙将nxm的方格分割成了若干个区域.现在要拆除一些墙,使其变成一个区域. 题目分析:将区域视作点 ...

  8. C#的157个建议--读书笔记(一)

    操作符的重载 ----实现类对象可以像值类型那样进行操作符的操作 实现比较器(IComparable)---实现对象的排序方式 协变:让返回值类型返回比声明的类型派生程度更大的类型:协变支持的两种方式 ...

  9. Python 基本语法1

    Python 基础语法(一) Python的特点 1. 简单 Python是一种代表简单思想的语言. 2. 易学 Python有极其简单的语法. 3. 免费.开源 Python是FLOSS(自由/开放 ...

  10. python设置字体颜色

    在开发项目过程中,为了方便调试代码,经常会向stdout中输出一些日志,默认的这些日志就直接显示在了终端中.而一般的应用服务器,第三方库,甚至服务器的一些通告也会在终端中显示,这样就搅乱了我们想要的信 ...