LintCode: coins in a line I
有 n 个硬币排成一条线。两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止。拿到最后一枚硬币的人获胜。
请判定 第一个玩家 是输还是赢?
n = 1, 返回 true.
n = 2, 返回 true.
n = 3, 返回 false.
n = 4, 返回 true.
n = 5, 返回 true.
解法:DP, 复杂度 O(N), O(N)
最少是n = 3时,返回false,说明当一个player面临只有3个棋子的时候,他肯定会输。
dp[i]表示的是,当有i个棋子的时候,先手玩家会不会输。dp[i]这个状态可以由dp[i - 1]或者dp[i - 2]跳过来。dp[i]赢得条件是,dp[i - 1]和dp[i - 2]的状态是输的状态。
可以优化空间复杂度到O(1)。
Java:
public boolean firstWillWin(int n) {
boolean[] dp = new boolean[n + 1];
for(int i = 1; i <= n; i ++) {
if(i == 1 || i == 2) dp[i] = true;
else dp[i] = !dp[i - 1] || !dp[i - 2];
}
return dp[n];
}
类似题目:
LintCode: coins in a line I的更多相关文章
- [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 ...
- 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, ...
- 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个石子之后都是必胜,则当前必败 ...
- 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 ...
- 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 ...
随机推荐
- word collocations中文版(信息检索)
虽然说是大作业,也做了好几天,但是完全没有什么实际价值...就是把现有的东西东拼西凑一下,发现跑的特别慢还搞了个多核 写这篇blog纯属是我吃饱了没事干,记录一下装env的蛋疼 首先我们是在pytho ...
- 洛谷 P1230 智力大冲浪 题解
P1230 智力大冲浪 题目描述 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者 \(m\)元.先不要太高兴!因为这些钱还不一定都是你的 ...
- 洛谷P1514引水入城
题目 搜索加贪心其实并不需要用到\(DP\),搜索也是比较简单地搜索. 对于每个第一行的城市进行类似于滑雪那道题的搜索,然后记录最后一行它所覆盖的区间,易得一个一行城市只会有一个区间.然后可以在最后进 ...
- Day16:小前端
回到顶部 <style type="text/css"> body { height: 8000px; } h1 { color: #000; } img { posi ...
- 如何修复GitKraken Inotify Limit Error\idea erro - 升级Ubuntu / Linux inotify限制
GitKraken是一个非常优秀的Git客户端.如果您是软件开发人员,那么您绝对应该试试GitKraken.今天我去了我的一个存储库做了一些提交,但是GitKraken告诉我它已经得到了Inotify ...
- (4)Go程序结构和流程控制
Go程序主要由以下几部分组成:(具体可以参考2选择结构中的实例) *包声明 *导入包 *函数 *变量 *语句和表达式 *注释 流程控制 1.顺序结构 2.选择结构 (1)if else if 和 e ...
- Pytest权威教程-更改标准(Python)测试发现
目录 更改标准(Python)测试发现 在测试收集过程中忽略路径 测试期间收集的测试取消 保留从命令行指定的重复路径 更改目录递归 更改命名约定 将cmdline参数解释为Python包 找出收集的东 ...
- cs 与 bs 架构
C/S和B/S各有优势,C/S在图形的表现能力上以及运行的速度上肯定是强于B/S模式的,不过缺点就是他需要运行专门的客户端,而且更重要的是它不能跨平台,用c++在windows下写的程序肯定是不能在l ...
- Python中_,__,__xx__方法区别
_xx 单下划线开头 Python中没有真正的私有属性或方法,可以在你想声明为私有的方法和属性前加上单下划线,以提示该属性和方法不应在外部调用.如果真的调用了也不会出错,但不符合规范. 方法就是以单下 ...
- FCN笔记
FCN.py tensorflow命令行参数 FLAGS = tf.flags.FLAGS tf.flags.DEFINE_integer("batch_size", " ...