lintcode:Coins in a Line 硬币排成线
题目
有 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 硬币排成线的更多相关文章
- [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 ...
- lintcode :Coins in Line II 硬币排成线 II
题目 硬币排成线 II 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币总价值,价值高的人获胜. 请判定 第一个玩家 是 ...
- LintCode: coins in a line I
有 n 个硬币排成一条线.两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止.拿到最后一枚硬币的人获胜. 请判定 第一个玩家 是输还是赢? n = 1, 返回 true.n = 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 ...
- LintCode之硬币排成线
输入的n可以分为两种情况: 1. 如果n是3的倍数的话,不论A怎么拿B都可以拿(3-A拿的个数)来使其保持是3的倍数,他就一定能拿到最后一块,所以n是3的倍数的话B必胜 2. 如果n不是3的倍数的话, ...
- LintCode "Coins in a Line III" !!
https://codesolutiony.wordpress.com/2015/05/24/lintcode-coins-in-a-line-iii/ A very juicy one! Deser ...
- LintCode "Coins in a Line II" !
Nice one to learn: DP + Game Theoryhttps://lefttree.gitbooks.io/leetcode/content/dynamicProgramming2 ...
- LintCode "Coins in a Line"
Recursion + Memorized Search(DP). And apparently, the code below can be iterative with only 3 vars - ...
- [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, ...
随机推荐
- 转:Android studio Gradle
提高Android Studio中Gradle执行效率 分类: android studio2015-06-26 11:54 2374人阅读 评论(2) 收藏 举报 android studiogra ...
- linux命令行解析函数介绍
函数原型: int getopt(int argc,char * const argv[ ],const char * optstring); 给定了命令参数的数量 ( ...
- SSL handshake failed: SSL 错误:在证书中检测到违规的密钥用法。
问题:在WINDOWS中创建的SVN Server,在Linux client中无法连接.原因:WINDOWS中的证书无法被Linux正确识别,因此需要修改证书,以使双方都可以正确识别. 修改方法如下 ...
- andriod Kernel configuration is invalid
error: ERROR: Kernel configuration is invalid. include/generated/autoconf.h or include/conf ...
- Spring4新特性简述
Spring是一个java世界中极其流行 的开源框架.Spring的初衷是降低企业级开发的复杂性,并试图通过POJO对象实现之前EJB这类重型框架才能实现的功能.Spring不仅仅对服务 端开发有用, ...
- .net speed up
Ngen.exe http://www.cnblogs.com/yukaizhao/archive/2011/11/07/how-to-use-ugen.html Merge.exe Merge dl ...
- 静态wenb开发,动态web开发
- 查看Linux磁盘空间大小
一.df 命令: df 是来自于coreutils 软件包,系统安装时,就自带的:我们通过这个命令可以查看磁盘的使用情况以及文件系统被挂载的位置: 举例: [root@localhost beinan ...
- try-catch-finally中return的执行情况分析
try-catch-finally中return的执行情况分析: 1.在try中没有异常的情况下try.catch.finally的执行顺序 try --- finally 2.如果try中有异常,执 ...
- 三大主流开源硬件对比:Arduino vs BeagleBone vs Raspberry Pi
个人总结: Arduino就是个AVR单片机,个人觉得更适合玩电子的,社区也很活跃. BeagleBone是ARM Cortex-A8,属于嵌入式,价格高于Pi,但是许多方面拥有超越 Pi 的优 势, ...