There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins.

Could you please decide the first play will win or lose?

Have you met this question in a real interview?

Yes
Example

n = 1, return true.

n = 2, return true.

n = 3, return false.

n = 4, return true.

n = 5, return true.

Challenge

O(n) time and O(1) memory

跟LeetCode上的那道Nim Game几乎一模一样。

解法一:

class Solution {
public:
/**
* @param n: an integer
* @return: a boolean which equals to true if the first player will win
*/
bool firstWillWin(int n) {
if (n <= ) return false;
if (n <= ) return true;
vector<bool> dp(n + , true);
dp[] = false;
for (int i = ; i <= n; ++i) {
dp[i] = dp[i - ];
}
return dp.back();
}
};

解法二:

class Solution {
public:
/**
* @param n: an integer
* @return: a boolean which equals to true if the first player will win
*/
bool firstWillWin(int n) {
return n % != ;
}
};

[LintCode] Coins in a Line 一条线上的硬币的更多相关文章

  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. 149. Max Points on a Line同一条线上的最多点数

    [抄题]: Given n points on a 2D plane, find the maximum number of points that lie on the same straight ...

  3. LintCode: coins in a line I

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

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

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

  7. img标签与span一起使用不在同一条线上

    布局时 img标签与span标签在同一行是不能垂直,解决办法:在 img标签添加一个 vertical-align:middle; 即 <!-- img与span的文字与图片保持同一条水平线 在 ...

  8. Tecplot如何提取三维图中某条线的数据【转载】

    转载自:http://blog.sina.com.cn/s/blog_9de422500102v9by.html 截取线所在的面Data.Extract .slice from Plane,显示如下窗 ...

  9. html+js+node实现五子棋线上对战,五子棋最简易算法

    首先附上我的github地址,https://github.com/jiangzhenfei/five,线上实例:http://47.93.103.19:5900/client/ 线上实例,你可以随意 ...

随机推荐

  1. [Unity3D插件]2dtoolkit系列二 动画精灵的创建以及背景图的无限滚动

    经过昨天2dtoolkit系列教程一的推出,感觉对新手还有有一定的启发作用,引导学习使用unity 2dToolKit插件的使用过程,今天继续系列二——动画精灵的创建,以及背景图的无限循环滚动,在群里 ...

  2. oc中定时器的基本使用

    // 时间间隔 调用的对象  调用的方法 用户信息 是否循环 [NSTimer scheduledTimerWithTimeInterval: target:self selector:@select ...

  3. Xcode 缓存 帮助文档 隐藏文件夹显示方法

    删除Xcode的缓存路径是:/Users/用户名/Library/Developer/Xcode/DerivedData(默认情况下, 这是一个隐藏文件夹) 显示隐藏文件夹 指令如下:显示隐藏文件: ...

  4. 驱动开发之 创建线程函数PsCreateSystemThread

    PsCreateSystemThread 创建一个执行在内核模式的系统线程. 注意:创建线程必须用函数PsTerminateSystemThread强制线程结束.否则该线程是无法自动退出的. 函数原型 ...

  5. CodeForces 19D Points(线段树+map)

    开始想不通,后来看网上说是set,就有一个想法是对每个x建一个set...然后又想直接建立两重的set就好,最后发现不行,自己想多了...  题意是给你三种操作:add (x y) 平面添加(x y) ...

  6. CSS3 calc()的使用

    前言: 平时在制作页面的时候,总会碰到有的元素是100%的宽度.众所周知,如果元素宽度为100%时,其自身不带其他盒模型属性设置还好,要是有别的,那将导致盒子撑破.比如说,有一个边框,或者说有marg ...

  7. UI中经常出现的下拉框下拉自动筛选效果的实现

    小需求是当你在第一个下拉框选择了国家时,会自动更新第二个省份的下拉框,效果如下 两个下拉选择Html如下: <select id="country_select"> & ...

  8. 树状数组+STL FZU 2029 买票问题

    题目传送门 题意:中文题面 分析:隔了一个考试周再做,开始没有什么思路,感觉能用线段树/树状数组维护,树状数组维护最小值不会去写线段树,结果超时.后来发现只要维护前缀几个人以及用优先队列/set维护最 ...

  9. Python学习笔记05

    类 继承:单继承,多继承 方法:self开头,类似C#中的this 属性:直接写变量 对象:使用类名() 构造 __init__():构造函数 #多继承 class A(object): ...... ...

  10. ural 1252. Sorting the Tombstones

    1252. Sorting the Tombstones Time limit: 1.0 secondMemory limit: 64 MB There is time to throw stones ...