Description

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 player will win or lose?

Example

n = 1, return true.

n = 2, return true.

n = 3, return false.

n = 4, return true.

n = 5, return true.

Challenge

O(n) time and O(1) memory

这个题目是属于经典的博弈类Dynamic Programming 的入门题目, 为了便于思考, 我们只考虑一个选手的status, 例如我们只考虑first player的状态, 然后 A[i] 表明到first player下时还

剩下i个coins, 找到动态关系式:    A[i] = (A[i-2] and A[i-3]) or (A[i-3] and A[i-4])    第一个是first player只选一个, 第二个是first player选2个之后由second player选了之后的到first player

下了的状态, 所以将second player的状态放入到了first player的状态当中. 此时需要注意的是初始化dp数组时, 要初始化0, 1,2, 3, 4

1. Constraints

1) n >= 0 integer

2. ideas

DP    T: O(n)   S; O(1)   optimal by rolling array

3. code

1) S: O(n)

class Solution:
def coinsInLine(self, n):
ans = [True]*5
ans[0] = ans[3] = False
if n < 5: return ans[n]
ans = ans + [None]*(n-4)
for i in range(5, n+1):
ans[i] = (ans[i-2] and ans[i-3]) or (ans[i-3] and ans[i-4])
return ans[n]

2) S: O(1)

class Solution:
def coinsInLine(self, n):
ans = [True] *5
ans[0] = ans[3] = False
if n < 5: return ans[n]
for i in range(5, n + 1):
ans[i%5] = (ans[i%5-2] and ans[i%5-3]) or (ans[i%5-3] and ans[i%5-4])
return ans[n%5]

3) 666

class Solution:
def coinsInLine(self, n):
return not (n%3 == 0)

4. Test cases

1) 0-6

[LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈的更多相关文章

  1. [LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈

    Description There are n coins with different value in a line. Two players take turns to take one or ...

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

  3. [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

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

  5. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  6. [LintCode] 77. Longest common subsequences_ Medium tag: Dynamic Programming

    Given two strings, find the longest common subsequence (LCS). Example Example 1: Input: "ABCD&q ...

  7. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming

    Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...

随机推荐

  1. python基础---->python的使用(七)

    这里记录python关于io.装饰器和序列化的一些知识.面对大河我无限惭愧,我年华虚度,空有一身疲倦,和所有以梦为马的诗人一样,岁月易逝 一滴不剩. python的一些知识 一.python中的装饰器 ...

  2. 【Java基础】System的arraycopy方法拷贝数组

    一.在System类中查看方法的定义 二.示例 public class SystemArrayCopyTest { /** * @Description: System的arrayCopy方法测试 ...

  3. c++ istream转换为std::string

    std::istreambuf_iterator<char> eos; std::string s(std::istreambuf_iterator<char>(stream) ...

  4. [原]git的使用(三)---管理修改、

    上接git的使用(二) 7.管理修改 [要理解的概念]为Git跟踪并管理的是修改,而非文件 什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一 ...

  5. Openstack 在VMware虚拟机ESXI和Workstation下安装需要更改参数

    [vmware vsphere] 要在esxi 5i的系统文件/etc/vmware/config最后添加vhv.allow = “TRUE” 一行.重启 VMware ESXi 后编辑虚拟机选项(需 ...

  6. MFC学习单选框Radio使用

    创建单选框Radio ,ID号IDC_RADIO_NAME 1.获取单选框内容 int RadioState = ((CButton *)GetDlgItem(IDC_RADIO_NAME))-> ...

  7. ssh免密码登录之ssh-keygen的用法

    A服务器:192.168.100.2 B服务器:192.168.100.3 要达到的目的:A服务器ssh登录B服务器不需要输入密码 1.在A服务器下使用ssh-keygen命令生成公钥/私钥密钥对,执 ...

  8. Spring Cloud Eureka 服务消费者

    参考<spring cloud 微服务实战> 现在已经构建了服务注册中心和服务提供中心,下面就来构建服务消费者: 服务消费者主要完成:发现服务和消费服务.其中服务的发现主要由Eureka的 ...

  9. CBV之详解

    一,CBV,基于反射实现根据请求方式不同,执行不同的方法. 1. 开发模式 - 普通开发方式(前后端放在一起写) - 前后端分离 2. 后端开发 为前端提供URL(API/接口的开发) 注:永远返回H ...

  10. bootstrap中点击左边展开

    <div id="sideNav" href=""><i class="material-icons dp48 glyphicon- ...