[LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈

Posted johnsonxiong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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 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, 博弈的主要内容,如果未能解决你的问题,请参考以下文章

Lintcode394 Coins in a Line solution 题解

[LintCode] Coins in a Line 一条线上的硬币

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

[LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈(

Coins in a Line

Coins in a Line II