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, ...
随机推荐
- 计算 sql查询语句所花时间
--1:下面这种是SQL Server中比较简单的查询SQL语句执行时间方法,通过查询前的时间和查询后的时间差来计算的: declare @begin_date datetimedeclare @en ...
- RHEL7 添加用户,含sudo权限
1.添加普通用户[root@server ~]# useradd book //添加一个名为book的用户 [root@server ~]# passwd book //修改密码 Changing p ...
- 为 WordPress 标签添加 rel="nofollow" 属性
WordPress 标签默认并无 rel="nofollow" 属性.rel="nofollow" 属性的作用是:告诉搜索引擎,无需追踪目标页,禁止蜘蛛爬行和传 ...
- How to modify Code Comments[AX2012]
// This is a framework class. Customizing this class may cause problems with future upgrades to the ...
- XAML 概述四
这一节我们来简单介绍一下XAML的加载和编译,它包括如下三种方式: · 只使用代码 · 使用代码和未编译的XAML · 使用代码和编译过的BAML 一. 只使用代码 我们首先创建一个简单的控制台 ...
- 斐波那契(Fibonacci)数列的七种实现方法
废话不多说,直接上代码 #include "stdio.h" #include "queue" #include "math.h" usin ...
- Nginx模块开发1_明白自定义模块的编译流程
自定义模块的编译流程 --add-module参数 configure使用--add-module参数指定添加模块目录. config脚本 由--add-module指定的目录保存为$ngx-addo ...
- 从Keil 4升级到Keil 5的工程,想返回来用Keil 4打开
情景描述: 笔者电脑程序Keil 4升级到Keil 5,相应地,原来项目上的工程也在第一次用Keil 5打开的时候进行了升级.之后,由于客户需要开发资料,其版本为Keil 4,我尝试着用Keil 4打 ...
- entityframwork
entityframwork映射部分: public class NorthwindContext : DbContext { public DbSet<CATEGORIES> Categ ...
- OpenWrt编译到底脚本
在办公室编译OpenWrt,费时很久,原因有两个. 一是办公室网络环境比较糟糕,经常断线不说,很多技术网站间歇性的连不上,不是撞到404就是DNS解析失败等. 二是初次编译OpenWrt时需要从网上下 ...