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. 今日Java——Cay Horstmann访谈

    这是本人在InfoQ中文站审校的文章,原文链接是:http://www.infoq.com/cn/articles/java_cay_horstmann,感觉内容很不错,分享给大家看看. 近日Info ...

  2. CacheDependency 的使用方法

    //创建缓存依赖项 CacheDependency dep = new CacheDependency(fileName); //创建缓存 HttpContext.Current.Cache.Inse ...

  3. PCL—低层次视觉—关键点检测(Harris)

    除去NARF这种和特征检测联系比较紧密的方法外,一般来说特征检测都会对曲率变化比较剧烈的点更敏感.Harris算法是图像检测识别算法中非常重要的一个算法,其对物体姿态变化鲁棒性好,对旋转不敏感,可以很 ...

  4. Adobe Acrobat Reader DC For Android 下载

    http://get.adobe.com/cn/reader/otherversions/ 点击“立即下载”按钮,即可开始下载到PC端

  5. vue使用sass

    一.安装sass依赖包 $ npm install sass-loader --save-dev //sass-loader依赖于node-sass $ npm install node-sass - ...

  6. SOS does not support the current target architecture解决方法

    客户提交一个dump文件,WinDbg加载时出现大量WARNING,加载对应版本的SOS后执行相应命令提示"SOS does not support the current target a ...

  7. .net C#中页面之间传值传参的六种方法

    1.QueryString是一种非常简单的传值方式,他可以将传送的值显示在浏览器的地址栏中.如果是传递一个或多个安全性要求不高或是结构简单的数值时,可以使用这个方法.但是对于传递数组或对象的话,就不能 ...

  8. Office word 2007不能另存为pdf格式的解决方法

    我们在使用Office word 2007时,经常会使用到另存为 PDF 或 XPS(P),遗憾的是,很多人都找不到这个选项, 或者在安装word的时候,并没有安装该加载项,需要你在后期安装,我们来怎 ...

  9. python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制

    一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这 ...

  10. css如何设置label的字间距

    css.html如何设置label的字间距 .myClass  label{ letter-spacing: 10px; } 如果label需要居中,需加上 text-indent: 10px; 首行 ...