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. java高级---->Thread之单例模式的使用

    这里我们介绍一下在多线程中如何安全正确的编写单例模式的代码.不知为何,恰如其分的话总是姗姗来迟,错过最恰当的时机. 多线程中的单例模式 这里面通过代码来体会一下在多线程中如何正确的编写单例模式的代码. ...

  2. 【linux系列】centos安装vsftp

    一.检查vsftpd软件 如果发现上不了网可以修改配置文件中的ONBOOT=no改为yes,然后重启服务试试

  3. Python tkinter 控件更新信息

    下面几个例子关于tkinter界面更新的,简单易懂,分享一下. 例子_1: 代码_1: from tkinter import Tk, Checkbutton, Label from tkinter ...

  4. vue项目打包之后页面空白解决办法

    之前项目遇到个情况,npm run build打包之后上传到服务器后,index.html打开一片空白,资源都加载了,但是就是不显示. 然后百度找了原因,修改了两处地方 一.修改 assetsPubl ...

  5. 题目1102:最小面积子矩阵(暴力求解&最大连续子序列)

    题目链接:http://ac.jobdu.com/problem.php?pid=1102 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  6. mysql select缓存使用详解

    mysql Query Cache 默认为打开.从某种程度可以提高查询的效果,但是未必是最优的解决方案,如果有的大量的修改和查询时,由于修改造成的cache失效,会给服务器造成很大的开销,可以通过qu ...

  7. 画一条0.5px的边

    1.scale方法 { height: 1px; transform: scaleY(0.5); transform-origin: 50% 100%; // 要指定origin值, 要不然会模糊 c ...

  8. elasticsearch的索引自动清理及自定义清理

    近发现elasticsearch近期索引文件大的吓人,清理了下之前的索引文件,发现服务器性能大大的减轻了一半,想一直保留近一个月的索引文件,但是又不想每个月手动清楚,在此写了一个小脚本 查询索引: c ...

  9. Windows Server 2008 R2之二从介质安装 AD DS

    可以使用 Ntdsutil.exe 为在域中创建的其他域控制器创建安装介质.通过从介质安装,可以最大程度地减少网络上目录数据的复制.有利于在远程站点中更高效地安装其他域控制器. 实验环境: 在Wind ...

  10. swiper监听左滑还是右滑动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...