【题目描述】

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. docker实践

    我的docker 学习笔记2   ps axf docker run -d cyf:sshd /usr/sbin -D   docker  ps docker-enter.sh 686 ps axf ...

  2. C#日志文件

    写日志文件是一个很常用的功能,以前都是别人写好的,直接调用的,近期写了一个小工具,因为比较小,所以懒得引用dll文件了,直接上网找了一个,很方便,现在记录下 public class LogClass ...

  3. ROS系统MoveIt玩转双臂机器人系列(一)

    一.ROS系统的MoveIt模块简介 机器人操作系统ROS目前最受关注的两个模块是导航(Navigation)和机械臂控制(MoveIt!),其中,机械臂控制模块(后面简称MoveIt)可以让用户快速 ...

  4. hdu1003 Max Sum---最大子段和+记录开始结束点

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意: 求最大子段和,并且输出最大子段和的起始位置和终止位置. 思路: 根据最大子段和基本 ...

  5. C++因继承引发的隐藏与重写

    在区分隐藏和重写之前,先来理一理关于继承的东西... [继承] 继承是面向对象复用的重要手段.通过继承定义一个类,继承是类型之间的关系建模,共享公有的东西,实现各自本质不同的东西.简单的说,继承就是指 ...

  6. java之设计模式工厂三兄弟之简单工厂模式

    [学习难度:★★☆☆☆,使用频率:★★★☆☆] 工厂模式是最常用的一类创建型设计模式,通常我们所说的工厂模式是指工厂方法模式,它也是使用频率最高的工厂模式.本章将要学习的简单工厂模式是工厂方法模式的& ...

  7. ·c#之Thread实现暂停继续(转)

    暂停与继续实现,可以使用Thread.Suspend和Thread.Resume而这两个方法,在VS2010里提示已经过时,不建议使用,在网上查阅了一些资料,发现有个事件通知的方法很好,事件通知的大致 ...

  8. Hive优化案例

    1.Hadoop计算框架的特点 数据量大不是问题,数据倾斜是个问题. jobs数比较多的作业效率相对比较低,比如即使有几百万的表,如果多次关联多次汇总,产生十几个jobs,耗时很长.原因是map re ...

  9. [LeetCode] String Compression 字符串压缩

    Given an array of characters, compress it in-place. The length after compression must always be smal ...

  10. [LeetCode] Contiguous Array 邻近数组

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...