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. WORD2007多级列表

    转自玄鸟翩翩 http://hi.baidu.com/shine_yen http://hi.baidu.com/shine_yen/item/01ff2255043bc1aeacc85722 用Wo ...

  2. whl文件安装

    进入whl文件的目录,直接pip install ...即可

  3. Mysql怎样取消错误命令

    1.补上分号. 2.quit 3.由于Mysql中,‘号和"号都是成对出现的,故当错误键入'号或"号时,需要补全另一半才能退出.

  4. Arduino101学习笔记(八)—— 函数库

    /*********最小值*********/ min() //实现:#define min(a,b) ((a)<(b)?(a):(b)) /*********最大值*********/ max ...

  5. centos7安装redis3.2.5

    安装redis 1官方介绍 Installation Download, extract and compile Redis with: $ wget http://download.redis.io ...

  6. mac/linux常用命令

    文件 创建文件: touch filename 创建目录: mkdir dirname, 创建目录及文件: mkdir -p dir/file 删除文件/目录: rm [-rf] filename 显 ...

  7. 简单的css 菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 并查集(路径更新) LA 3027 Corporative Network

    题目传送门 题意:训练指南P192 分析:主要就是一个在路径压缩的过程中,更新点i到根的距离 #include <bits/stdc++.h> using namespace std; c ...

  9. ccc progressbar

    cc.Class({ extends: cc.Component, properties: { progressBar: { default:null, type:cc.ProgressBar }, ...

  10. 一些有用的HTML5 pattern属性

    最近在做手机页面时,遇到数字输入的键盘的问题,之前的做法只是一刀切的使用 type="tel",不过一直觉得九宫格的电话号码键盘上的英文字母太碍事了.于是想要尝试其它的实现方案,最 ...