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. 【Spring】依赖注入 加载顺序

    一.Spring依赖注入depents-on参数 depents-on是指指定Bean初始化及销毁时的顺序,使用depends-on属性指定的是Bean要先初始化完毕后才初始化当前Bean,由于只有S ...

  2. css笔记 - 张鑫旭css课程笔记之 z-index 篇

    一.z-index语法.支持的属性值等 z-index: 在支持z-index的元素上, z-index规定了元素(包括子元素)的垂直z方向的层级顺序, z-index可以决定哪个元素覆盖在哪个元素上 ...

  3. 原生js--表单

    阅读了<javascript权威指南>P396-P409. 一.表单和表单元素的选取 1.选取表单(包含name=“address”属性的form表单) document.querySel ...

  4. sencha touch list 选择插件,可记忆已选项,可分组全选

    选择记忆功能参考:https://market.sencha.com/extensions/ext-plugin-rememberselection 插件代码: /* * 记住列表选择状态 * 如果分 ...

  5. sencha touch 2.2 为list PullRefresh插件添加refreshFn方法

    sencha touch 2.2 list PullRefresh插件没有refreshFn方法 但是我们又需要他,所以需要自行扩展 代码如下 /** * 重写下拉刷新插件,以支持refreshFn事 ...

  6. sencha touch 视图(view) activate与deactivate事件探讨

    在sencha touch2.2中采用card布局 之前的需求是考虑show,hide事件发现不可取 http://www.cnblogs.com/mlzs/archive/2013/06/13/31 ...

  7. Windows正向绑定shell和反向反弹shell的Python代码

    Windows下的shell原理 经过查阅资料,使用os.dup2(nfd, ofd)的方式重定向socket的输入输出到windows系统的cmd是无法做到的,属于系统原因,不能直接复制Linux下 ...

  8. linux系统中关于shell变量$*与$@的区别

    在我们初学linux系统shell时,可能会感觉$@与$*没什么区别,如下面shell脚本: #!/bin/bash# name:a.sh # echo 'this script $* is: '$* ...

  9. 有哪些sql优化工具

    https://www.oschina.net/p/soar-xiaomi https://www.oschina.net/news/101034/xiaomi-opensource-soar SOA ...

  10. 8.23 js

    2018-8-23 15:12:05 js 参考 :https://www.cnblogs.com/liwenzhou/p/8011504.html 2018-8-23 20:56:29 上面js的东 ...