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实战案例(2)实例讲解使用软连接的场景和过程
=================================== 使用场景:使用软连接简化版本切换动作 进入操作目录, cd /opt/modules/ ==================== ...
- Python 中格式化字符串 % 和 format 两种方法之间的区别
Python2.6引入了 format 格式化字符串的方法,现在格式化字符串有两种方法,就是 % 和 format ,具体这两种方法有什么区别呢?请看以下解析. # 定义一个坐标值 c = (250, ...
- Centos6.9minimal安装图形化界面
有时我们会用到图形化界面来操作,下面介绍是在虚拟机上安装Centos6.9minimal版安装图形化界面(其他系统版本都类似吧,,,),如果是在物理机上安装进入的话要用的远程桌面工具VNC. VNC安 ...
- 【第二十一篇】手C# MVC 微信授权登录 OAuth2.0授权登录
首先一定要熟读,最起码过一遍微信开发者文档 微信开发者文档 文档写的很清楚 授权登录四步走 在正文开始前,我得讲清楚一个事情 敲黑板,划重点:微信一共有两个 access_token 一个是7200就 ...
- Centos7 安装python3
Centos7 安装python3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #安装sqlite-devel yum -y ...
- 1.3WEB API 默认以json格式返回数据,同时定义时间格式,返回格式
首先我们知道,web api 是可以返回任意类型的,然后在输出的过程中转为(默认的)xml. 但是xml是比较费流量的,而且大多前端都是用json对接,所以我们也只能随大流,把它输出改成json. 不 ...
- requests+正则表达式提取猫眼电影top100
#requests+正则表达式提取猫眼电影top100 import requests import re import json from requests.exceptions import Re ...
- cuda小白基础教程
一直很想做cuda-GPU编程,很早就将CUDA9.0安装好了,后面就没怎么管它,忙别的去了.敲黑板,划重点,我科研还是很努力的,可是很多人看不见罢了.之前一直在使用粒子方法进行流体模拟,计算时间极其 ...
- linux下nginx负载均衡搭建
[一.Nginx能做什么] 1.http服务器.Nginx是一个http服务可以独立提供http服务.可以做网页静态服务器. 2.虚拟主机.可以实现在一台服务器虚拟出多个网站.例如个人网路使用的虚拟主 ...
- 八:Vue下的国际化处理
p { margin-bottom: 0.25cm; line-height: 120% } 1:首先安装 Vue-i8n npm install vue-i18n --save 注:-save-de ...