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. Qt编写气体安全管理系统(界面超漂亮)

    自从把Qt样式表葵花宝典这个pdf文件看完以后,将所有的qss内容都轮了一遍,还写了个皮肤生成器工具,https://blog.csdn.net/feiyangqingyun/article/deta ...

  2. laravel + php cgi + nginx在windows平台下的配置

    1.d:\xampp\php\php-cgi.exe -b 127.0.0.1:9000 -c d:\xampp\php\php.ini 2.nginx conf配置如下: #user nobody; ...

  3. Elasticsearch学习之深入搜索二 --- 搜索底层原理剖析

    1. 普通match如何转换为term+should { "match": { "title": "java elasticsearch"} ...

  4. [转]13 Hours: The Secret Soldiers of Benghazi

    转:http://www.imfdb.org/wiki/13_Hours:_The_Secret_Soldiers_of_Benghazi The following weapons were use ...

  5. MySQL对数据表进行分组查询(GROUP BY)

    MySQL对数据表进行分组查询(GROUP BY) GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...

  6. 7.18 进程池方式的 socket

    2018-7-18 22:05:28  笔记都做完了 睡觉睡觉!!!明天早起!发现python慢慢深入变得很强大,很好玩!!! 进程池版本的socket 更加优化了,比多进程版本的效率更高 seerv ...

  7. Druid在有赞的实践

    转载一篇自己在公司博客上的文章 一.Druid介绍 Druid 是 MetaMarket 公司研发,专为海量数据集上的做高性能 OLAP (OnLine Analysis Processing)而设计 ...

  8. poj1611 The suspects【并查集】

    严重急性呼吸系统综合症( SARS), 一种原因不明的非典型性肺炎,从2003年3月中旬开始被认为是全球威胁.为了减少传播给别人的机会, 最好的策略是隔离可能的患者. 在Not-Spreading-Y ...

  9. HDU 2544 - 最短路 - [堆优化dijkstra][最短路模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 Time Limit: 5000/1000 MS (Java/Others) Memory Li ...

  10. Codeforces Round #439 (Div. 2)

    A. The Artful Expedient 题目链接:http://codeforces.com/contest/869/problem/A 题目意思:给你两个数列,各包含n个数,现在让你从上下两 ...