题目

有 n 个硬币排成一条线。两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止。拿到最后一枚硬币的人获胜。

请判定 第一个玩家 是输还是赢?

样例

n = 1, 返回 true.

n = 2, 返回 true.

n = 3, 返回 false.

n = 4, 返回 true.

n = 5, 返回 true.

挑战

O(1) 时间复杂度且O(1) 存储。

解题

两个人在一次拿的时候,当第一个人拿的是1 时,第二个人拿的就是2;当第一个人拿的是2时,第二个人拿的就是1。这样一次拿取得值是3.这样就是一个以3为周期的序列,当只剩三个硬币的时候,不傻也知道怎么拿的。如下,直接对只有三枚硬币的情况考虑即可。

n = 1, 返回 true.

n = 2, 返回 true.

n = 3, 返回 false.

注意:

取硬币格式的序列可以是:(1,2),(1,2),(2,1),(1,2)而不是严格的按照(1,2)(1,2)的序列取硬币

解题

根据上面给的样例,很简单的写出下面程序了

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

当然也可以递归的

public class Solution {
/**
* @param n: an integer
* @return: a boolean which equals to true if the first player will win
*/
public boolean firstWillWin(int n) {
// write your code here
if(n==1||n==2)
return true;
if(n==0 ||n==3)
return false;
return firstWillWin(n-3);
}
}

法二

参考链接

根据上面的程序,感觉是直接推出来的规律

  第2个人 第1个人
1 F T
2 F T
3 T F
4 F T
5 F T
6 T F
7 F T
8 F T
9 T F

当 n大于3的时候

对第0列,也就是第二个人的结果是:D[i][0] = D[i-1][1] && D[i-2][1]

对第1列,也就是第一个人的结果是:D[i][0] = D[i-1][0] || D[i-2][0]

说明当前人能否获胜收到另外一个人连续两次取值的影响,与上面通过找规律的结果其实也是一样的。

public class Solution {
/**
* @param n: an integer
* @return: a boolean which equals to true if the first player will win
*/
public boolean firstWillWin(int n) {
// write your code here
if( n <=0 ) return false;
if(n <=2 ) return true;
boolean [][] D = new boolean[n+1][2];
D[1][1] = true;
D[2][1] = true;
D[1][0] = false;
D[2][0] = false;
for(int i = 3 ;i<= n ;i++){
for( int j = 0;j<=1;j++){
if( j==1 ){
D[i][j] = D[i-1][1-j] || D[i-2][1-j];
}else{
D[i][j] = D[i-1][1-j] && D[i-2][1-j];
}
}
}
return D[n][1];
}
}

Java Code

总耗时: 807 ms

lintcode:Coins in a Line 硬币排成线的更多相关文章

  1. [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 ...

  2. lintcode :Coins in Line II 硬币排成线 II

    题目 硬币排成线 II 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币总价值,价值高的人获胜. 请判定 第一个玩家 是 ...

  3. LintCode: coins in a line I

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

  4. [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 ...

  5. LintCode之硬币排成线

    输入的n可以分为两种情况: 1. 如果n是3的倍数的话,不论A怎么拿B都可以拿(3-A拿的个数)来使其保持是3的倍数,他就一定能拿到最后一块,所以n是3的倍数的话B必胜 2. 如果n不是3的倍数的话, ...

  6. LintCode "Coins in a Line III" !!

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

  7. LintCode "Coins in a Line II" !

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

  8. LintCode "Coins in a Line"

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

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

随机推荐

  1. NaN 和 Infinity

    using Fasterflect; using System; using System.Collections.Generic; using System.Linq; using System.R ...

  2. Hadoop介绍及最新稳定版Hadoop 2.4.1下载地址及单节点安装

     Hadoop介绍 Hadoop是一个能对大量数据进行分布式处理的软件框架.其基本的组成包括hdfs分布式文件系统和可以运行在hdfs文件系统上的MapReduce编程模型,以及基于hdfs和MapR ...

  3. VirtrualBox 搭建本地lamp环境

    1.VirtrualBox安装Centos6.8 minimal VirtrualBox新建个虚拟机配置好内存以及硬盘大小,安装即可: 网络方式是 NAT(默认)和桥接方式来实现,最好在安装前设置好, ...

  4. CodeForces 478B 第八次比赛 B题

    Description n participants of the competition were split into m teams in some manner so that each te ...

  5. 从零开始学ios开发(十九):Application Settings and User Defaults(上)

    在iphone和ipad中,有一个东西大家一定很熟悉,那个东西就是Settings. 这次要学习的东西说白了很简单,就是学习如何在Settings中对一个app的某些属性进行设置,反过来,在app中更 ...

  6. C++中使用心得

    1.struct成员默认访问方式是public,而 class默认访问方式是private! 2.exit函数终止程序执行会调用析构函数 ,abort函数终止程序不会调用析构函数! 3.静态局部变量直 ...

  7. WPF 多线程处理(6)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 以下是子窗体的UI: <Window ...

  8. 笔记本电脑连接wifi有时候会自动断网提示有限的访问权限解决办法

    解决办法如下: [设备管理器],找到[网络适配器]第一项,右键属性

  9. 2877: [Noi2012]魔幻棋盘 - BZOJ

    DescriptionInput 第一行为两个正整数N,M,表示棋盘的大小. 第二行为两个正整数X,Y,表示棋盘守护者的位置. 第三行仅有一个正整数T,表示棋盘守护者将进行次操作. 接下来N行,每行有 ...

  10. 1458: 士兵占领 - BZOJ

    Description 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放 ...