【题目描述】

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)。

【参考答案】

www.jiuzhang.com/solutions/coins-in-a-line/

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Lintcode394 Coins in a Line solution 题解的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. [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, ...

  6. 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 ...

  7. Coins in a Line

    Description There are n coins in a line. Two players take turns to take one or two coins from right ...

  8. 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个石子之后都是必胜,则当前必败 ...

  9. 396. Coins in a Line III

    刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...

随机推荐

  1. redis入门(05)redis的key命令

    一.什么是redis键命令 Redis 键(key):Redis 键命令用于管理 redis 的键. Redis 键命令的基本语法: redis 127.0.0.1:6379> COMMAND ...

  2. leetcode算法: Keyboard Row

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  3. vue组件详解(五)——组件高级用法

    一.递归组件 组件在它的模板内可以递归地调用自己, 只要给组件设置name 的选项就可以了. 示例如下: <div id="app19"> <my-compone ...

  4. url的解码方式

    #coding:utf-8 import urllib legal_person_string = "%E6%B3%95%E5%AE%9A%E4%BB%A3%E8%A1%A8%E4%BA%B ...

  5. Html5 监听拦截Android返回键方法详解

    浏览器窗口有一个history对象,用来保存浏览历史. 如果当前窗口先后访问了三个网址,那么history对象就包括三项,history.length属性等于3. history对象提供了一系列方法, ...

  6. NSDateFormatter常见的使用方式

    NSDateFormatter是NSFormatter的子类,是用来表示输出的时间格式. 下面贴出两个时间的例子. 例子一. - (void)dateString1 { NSDateFormatter ...

  7. github git 在GitHub上创建项目并将本地项目push到网站上

    众所周知,git是与svn类似的版本控制系统,git的去中心化.分布式等的优点,在不久将来用户量大有可能超过svn, 常见的代码托管网站有GitHub,coding.net, gitee.com 码云 ...

  8. [LeetCode] Max Consecutive Ones 最大连续1的个数

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  9. WebRTC 音频采样算法 附完整C++示例代码

    之前有大概介绍了音频采样相关的思路,详情见<简洁明了的插值音频重采样算法例子 (附完整C代码)>. 音频方面的开源项目很多很多. 最知名的莫过于谷歌开源的WebRTC, 其中的音频模块就包 ...

  10. Scrapy定时执行爬取任务与定时关闭任务

    当我们利用Python scrapy框架写完脚本后,脚本已经可以稳定的进行数据的爬取,但是每次需要手动的执行,太麻烦,如果能自动运行,在自动关闭那就好了,经过小编研究,完全是可以实现的,今天小编介绍2 ...