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 until there are no more coins left. The player who take the last coin wins.
Could you please decide the first play will win or lose?
有 n 个硬币排成一条线。两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止。拿到最后一枚硬币的人获胜。
请判定 第一个玩家 是输还是赢?
【题目链接】
www.lintcode.com/en/problem/coins-in-a-line/
【题目解析】
其实此问题如果从另一个角度思考,就是从最后剩余1个或2个硬币时进行倒推,寻找规律:
先手输:
o o o | o o o
先手胜:
o | o o o
制胜的方法就是一定在倒数第二个回合时,让对手面对3个硬币,这样因为自己可以拿1或者2个硬币,那么无论对手选1个或者2个,己方都可以拿到最后一个硬币。这个规律就是每次让对手都面对3的倍数个硬币,那么无论对方取1个或者2个,只需要取相应的硬币数,让剩下的硬币数目保持3X,这样就能够保证取胜。对于先手而言,如果自己第一轮面对的就是3的倍数个硬币,那么对手则可以使用同样的策略让自己一方每次面对3X个硬币。于是先手是否获胜的唯一要素就是初始硬币数目,在不为3的整数倍情况下,先手都可以获胜。这样的话,算法时间复杂度和空间复杂度都为O(1)。
【参考答案】
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
Lintcode394 Coins in a Line solution 题解的更多相关文章
- [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 ...
- 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, ...
- Coins in a Line III
Description There are n coins in a line, and value of i-th coin is values[i]. Two players take turns ...
- Coins in a Line
Description There are n coins in a line. Two players take turns to take one or two coins from right ...
- 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个石子之后都是必胜,则当前必败 ...
- 396. Coins in a Line III
刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...
随机推荐
- Linux知识积累(3)$()和${}和$(())和(())
$()和${}和$(())和(()) $()和${}的用法:在 bash shell 中,$( ) 与 ` ` (反引号) 都是用来做命令替换用(command substitution)的.而 $( ...
- 新概念英语(1-39)Don't drop it!
新概念英语(1-39)Don't drop it! Where does Sam put the vase in the end ? A:What are you going to do with t ...
- 新概念英语(1-67)The weekend
新概念英语(1-67)The weekend What are the Johnsons going to do at the weekend? A:Hello. Were you at the bu ...
- SublimeText用FileHeader给代码文件生成头部注释
https://github.com/shiyanhui/FileHeader 修改的模板在这个路径下:C:\Users\[USERNAME]\AppData\Roaming\Sublime Text ...
- ASC学习笔记
TCL:(Tool Command Language), a computer programming languagecharm++:基于C++的面向对象的并行编程语言.Charm++ is a p ...
- H5 input输入限制最大位数,和调用小键盘需求发生冲突的解决办法
首先,限制输入最大位数时,input有自带的属性maxlength. <input type="text" name="email" maxlength= ...
- 初学Java Web(9)——学生管理系统(简易版)总结
项目开始时间:2018年4月8日14:37:47 项目完成时间:2018年4月9日10:03:30 技术准备 这个项目是自己用于巩固 J2EE 相关知识的练手项目,非常简单,但是相关的功能却非常实用, ...
- 原生js中实现全选和反选功能
<!DOCTYPE html> <html> <head lang="en"> <meta char ...
- 在MySQL中使用子查询
子查询作为数据源 子查询生成的结果集包含行.列数据,因而非常适合将它与表一起包含在from子句的子查询里.例: SELECT d.dept_id, d.name, e_cnt.how_many num ...
- Java集合总结【面试题+脑图】,将知识点一网打尽!
前言 声明,本文用的是jdk1.8 花了一个星期,把Java容器核心的知识过了一遍,感觉集合已经无所畏惧了!!(哈哈哈....),现在来总结一下吧~~ 回顾目录: Collection总览 List集 ...