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. C/C++注册动态对象到Lu系统并进行运算符重载

    欢迎访问Lu程序设计 C/C++注册动态对象到Lu系统并进行运算符重载 1 说明 要演示本文的例子,你必须下载Lu32脚本系统.本文的例子需要lu32.dll.lu32.lib.C格式的头文件lu32 ...

  2. 【大数据系列】hadoop单机模式安装

    一.添加用户和用户组 adduser hadoop 将hadoop用户添加进sudo用户组 sudo usermod -G sudo hadoop 或者 visudo 二.安装jdk 具体操作参考:c ...

  3. mFC 橡皮线

    一般都用GDI实现: void CXiangpijinView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message ...

  4. enum hack

    关于占用内存的大小,enum类型本身是不占内存的,编译器直接替换.但是enum类型的变量肯定是占内存的. class A{ public: //enum类型本身不占内存 enumEnumTest{ a ...

  5. 写一个自定义的控件接口 C#

    以下是我的测试代码:APP_Code/ucInterface.cs /* APP_Code/ucInterface.cs */ /// <summary> /// Summary desc ...

  6. 部署OpenStack问题汇总(一)--使用packstack安装openstack:源问题的处理

    在安装的过程中,遇到了源的问题,找不到包的网页:    重新打开 预装源地址,打开epel-openstack-havana.repo 文件,显示如下: # Place this file in yo ...

  7. JVM工具jcmd实践

    暂时参考以下链接,后续补充自己的实践. https://www.jianshu.com/p/388e35d8a09b

  8. nginx命令行参数和信号

    nginx命令行参数 [user@host dir]$ /usr/local/nginx/sbin/nginx -hnginx version: nginx/1.8.0Usage: nginx [-? ...

  9. Linux下使用 xrandr 命令设置屏幕分辨率

    最近在Linux下修改屏幕分辨率的时候,发现了一个非常有用的命令:xrandr 使用这个命令,可以方便的设置您显示器的的分辨率.尤其是当你使用了一些需要或者会自动改动您屏幕分辨率的程序以后. 您可以使 ...

  10. CentOS系统下docker的安装与卸载

    Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制, ...