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, ...
随机推荐
- mvc涉及到input设置了disabled
在做网站管理后台的用户修改功能时,由于当前用户修改个人信息时规定用户名不能修改,故使用了input标签的disabled属性,但是在提交数据后却发现用户名显示为空了.后来一查才知道input设置为di ...
- Ubuntu 12.04 修改默认启动为字符界面
sudo vim /etc/default/grub 修改GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" 为:GRUB_CMDLINE_LINU ...
- 使用cookie解决微信不能存储localStorage的问题
最近在开发基于微信的Web页面时,发现有些机型不能存储信息到localStorage中,或者是页面一旦关闭,存储的信息也失效了. 于是想到用cookie来替代localStorage,存储一些简单的数 ...
- @RenderSection与@RenderBody
_LayoutMain: <html> <head> @RenderSection("head") </head> <body> @ ...
- opencv学习笔记(05)——操作相邻区域
下面的例子以灰度图像为例: #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp& ...
- BZOJ 4031: [HEOI2015]小Z的房间 Matrix-Tree定理
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=4031 题解: Matrix-tree定理解决生成树计数问题,其中用到高斯消元法求上三角矩 ...
- C# Socket连接超时设置
问题描述: 对于C# Socket没有超时设置的选项,默认情况下进行Socket连接,返回连接失败需要20-30s时间,严重影响用户体验 问题解决: Socket服务器端: Socke ...
- 【BZOJ】【1412】【ZJOI2009】狼和羊的故事
网络流/最小割 一开始我是将羊的区域看作连通块,狼的区域看作另一种连通块,S向每个羊连通块连一条无穷边,每个狼连通块向T连一条无穷边,连通块内部互相都是无穷边.其余是四连通的流量为1的边……然后WA了 ...
- git如何ignore
今天新建了一个项目传到git上,但是每次编译都会有一些无用的文件生成,于是就编写了ignore.但是发现无用.因为你的文件已经上传到服务器了,再编写ignore就无用了,ignore的适用是文件没上传 ...
- 无法从 ajax.googleapis.com 下载问题
除FQ外的解决办法: 打开目录 C:\Windows\System32\drivers\etc,修改 hosts 文件,添加一行 : 127.0.0.1 ajax.googleapis.com 打开I ...