394. Coins in a Line
最后更新
一刷。
用数学方法是看是不是3的倍数。
不用数学方法的话要动态规划。
当前玩家,dp[i]行不行取决于dp[i-1]和dp[i-2],代表下一个玩家能不能赢,另一个玩家能赢的话当前就不能赢;另一个玩家不能赢,当前就能赢。
因为当前玩家可以通过拿1个或者拿2个来左右结果。
dp[i] = !dp[i-1] || !dp[i-2];
然后滚动数组滚起来。。
public class Solution {
public boolean firstWillWin(int n) {
if (n == 0) return false;
boolean[] dp = new boolean[3];
dp[0] = true;
dp[1] = true;
dp[2] = false;
for (int i = 3; i < n; i++) {
dp[i%3] = !dp[(i-1)%3] || !dp[(i-2)%3];
}
return dp[(n-1)%3];
}
}
394. Coins in a Line的更多相关文章
- lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II
变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...
- [LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈
Description There are n coins with different value in a line. Two players take turns to take one or ...
- [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] 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 ...
- LeetCode 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 ...
- Lintcode394 Coins in a Line solution 题解
[题目描述] There are n coins in a line. Two players take turns to take one or two coins from right side ...
- Coins in a Line I & II
Coins in a Line I There are n coins in a line. Two players take turns to take one or two coins from ...
- [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, ...
- [LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈
Description There are n coins in a line. Two players take turns to take one or two coins from right ...
随机推荐
- textarea 超过字数
<textarea name="></textarea> <div id="statementRowChk"></div> ...
- php表单发送到邮箱V1.0
html表单代码: <form action="index.php" name="form" method="POST"> &l ...
- centos nginx
1.关闭SELinux 查看SELinux状态: (1)/usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态 SELinux status: ...
- 强大DevExpress,Winform LookUpEdit 实现多列查询 gridview弹出下拉选择 z
关键代码请参考http://www.devexpress.com/Support/Center/p/K18333.aspx 最新DEMO 下载 The current GridLookUpEdit's ...
- after I see Little Dorrit
也许是我太追名逐利,所以我不肯承认自己花费了大把的时间看电影,通过写博客好像自己从中感悟到了什么,好像看电影也是一种学习的方式. 也许是我平静自内心的方式,我太忙于玩或者学习,甚至没有机会非常沉静 一 ...
- POJ 3687 Labeling Balls 逆向建图,拓扑排序
题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...
- Spring实战——无需一行xml配置实现自动化注入
已经想不起来上一次买技术相关的书是什么时候了,一直以来都习惯性的下载一份电子档看看.显然,如果不是基于强烈的需求或强大的动力鞭策下,大部分的书籍也都只是蜻蜓点水,浮光掠影. 就像有位同事说的一样,有些 ...
- 定位 - CoreLocation - 区域报警
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface ViewCont ...
- 修改.htaccess实现子目录绑定示例分享
<IfModule mod_rewrite.c>RewriteEngine On RewriteBase /# 把 www.jb51.net改为你要绑定的域名.# 如果是域名:Rewri ...
- SessionId
http://www.codeweblog.com/session-cookie-jsessionid-url-rewriting/