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 ...
随机推荐
- wcf通道Channel
正文 客户端与服务进行交互的过程是通过通道进行交互的.客户端通过调用代理类执行相应的方法,通过通道编码,调用上下文,传输客户端的事务,管理可靠会话,对消息正文的加密,最后要执行的通道是传输通道就像我们 ...
- PHP构造函数的用法分析
PHP构造函数的声明与其它操作的声明一样,只是其名称必须是__construct( ).这是PHP5中的变化,以前的版本中,构造函数的名称必须与类名相同,这种在PHP5中仍然可以用,但现在以经很少有人 ...
- Custom template tags and filters
Code layout Custom template tags and filters must live inside a Django app. If they relate to an exi ...
- jquery mobile页面跳转后,必须重新刷新页面js方可有效
最近在做个项目,用到jquery mobile,很陌生对他,问题一个个的来,那就要一个个解决,找了一天这个问题,放可明白:首先明白jqm里面页面跳转默认都是通过ajax请求的,必须重新刷新页面js方可 ...
- HttpContext.Current.Session=null问题
启用asp.net状态服务,可以让Session持久化!
- jdk .tar.gz 包安装 inAction
方法参考于http://blog.sina.com.cn/s/blog_81631744010137iy.html, 多谢博主分享,此处仅作为个人学习笔记. 首先将*.tar.gz解压 tar -xz ...
- HDU 1532 Drainage Ditches 最大流 (Edmonds_Karp)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 感觉题意不清楚,不知道是不是个人英语水平问题.本来还以为需要维护入度和出度来找源点和汇点呢,看 ...
- app应用程序本地化--备用
一.简介 * 使用本地化功能,可以轻松地将应用程序翻译成多种语言,甚至可以翻译成同一语言的多种方言 * 如果要添加本地化功能,需要为每种支持的语言创建一个子目录,称为”本地化文件夹”,通常使用.lpr ...
- 第 8 章 适配器模式【Adapter Pattern】
以下内容出自:<<24种设计模式介绍与6大设计原则>> 好,请安静,后排聊天的同学别吵醒前排睡觉的同学了,大家要相互理解嘛.今天讲适配器模式,这个模式也很简单,你笔记本上的那个 ...
- CKplayer 新手入门超简单使用教程
网页播放器都有使用的前提(问1). ~~~~~~~分隔线~~~~~~~ 只需一步先看播放器效果(问2): 下载附件,解压内容(ckplayer文件夹和ckplayer.html)到网站根目录,在浏览器 ...