Description

There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins.

Could you please decide the first player will win or lose?

Example

Given values array A = [1,2,2], return true.

Given A = [1,2,4], return false.

这个题目思路实际上跟[LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈很像, 然后博弈类的如果要取最大值, 需要用minmax算法, 得到关系式

A[i] = max(min(ans[i-2], ans[i-3]) + values[n-i], min(ans[i-3], ans[i-4] + values[n-i] + values[n-i+1])) ,

第一个就是只选1个coin, 第二个就是选两个coins, 最后return ans[n] > sum(values) //2

1. Constraints

1) values 长度大于等于0

2) element 是大于0 的integer

2. Ideas

Dynamic Programming      T: O(n)           S; O(n)  optimal  O(1)

3. Code

1) S; O(n)

class Solution:
def coinsInLine2(self, values):
ans = [0]*5
ans[1]= values[0]
ans[2] = ans[3] = sum(values[:2])
n = len(values)
if n < 4: return ans[n] > sum(values) //2
ans[4] = values[0] + max(values[1], values[3])
ans = ans + [0]*(n-4)
for i in range(5, n+1):
ans[i] = max(min(ans[i-2], ans[i-3]) + values[n-i], min(ans[i-3], ans[i-4]) + values[n-i] + values[n-i+1])
return ans[n] > sum(values)//2

2) 可以用滚动数组方式将Space降为O(1)

4. Test cases

1) [1,5,2,10]

[LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈的更多相关文章

  1. [LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈

    Description There are n coins in a line. Two players take turns to take one or two coins from right ...

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

  4. [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 ...

  5. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

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

  6. [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 ...

  7. [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. [LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  9. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

随机推荐

  1. jquery.flot.js简介

    JQuery图表插件之Flot Flot是一个Jquery下图表插件,具有简单使用,交互效果,具有吸引力外观特点.目前支持 Internet Explorer 6+, Chrome, Firefox ...

  2. 【Spring Boot&& Spring Cloud系列】单点登录SSO之OAuth2官方开发文档翻译

    Introduction:介绍 This is the user guide for the support for OAuth 2.0. For OAuth 1.0, everything is d ...

  3. 原生js--类的扩充和类型检测

    扩充类的方法: 1.向原型对象上添加方法或属性.例如:Number.prototype.cl = function(){}; 但这种做法并不推荐,因为ES5之前,无法将这些新添加的方法或属性设置为不可 ...

  4. 使用mimikatz获取和创建Windows凭据的工具和方法

    Mimikatz 下载地址 https://github.com/gentilkiwi/mimikatz/releases 本地凭据破解 以管理员身份运行(拿到shell提权后) mimikatz#p ...

  5. 微信公众号关联(小游戏 小程序 跳转 盒子 wx.navigateToMiniProgram)

    参考: 公众号关联小程序 关联公众号 关联后,登录小游戏,可在设置-关联设置中看到关联的公众号 在小游戏中使用wx.navigateToMiniProgram wx.navigateToMiniPro ...

  6. Docker Swarm——集群管理

    前言 之前在总结docker machine的时候,当时对docker理解还不够深入,甚至还不知道 docker machine 与 docker swarm 的区别. 在查阅资料以及官方文档之后,今 ...

  7. 高斯混合模型Gaussian Mixture Model (GMM)

    混合高斯模型GMM是指对样本的概率密度分布进行估计,而估计采用的模型(训练模型)是几个高斯模型的加权和(具体是几个要在模型训练前建立好).每个高斯模型就代表了一个类(一个Cluster).对样本中的数 ...

  8. vue之创建组建

    vue的核心基础就是组件的使用,玩好了组件才能将前面学的基础更好的运用起来.组件的使用更使我们的项目解耦合.更加符合vue的设计思想MVVM. 那接下来就跟我看一下如何在一个Vue实例中使用组件吧! ...

  9. CH6101 最优贸易【最短路】

    6101 最优贸易 0x60「图论」例题 描述 C国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通 ...

  10. 单表行数超过 500 万行或者单表容量超过 2GB,才推荐进行分库分表。

    https://github.com/alibaba/p3c/blob/master/阿里巴巴Java开发手册(详尽版).pdf 单表行数超过 500 万行或者单表容量超过 2GB,才推荐进行分库分表 ...