[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]

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9369191.html

时间: 2025-01-07 01:14:30

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

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

[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, and each pile has a positive integer number of stones piles[i]. The objective of the game is to end with the most stones.  The total number of stones

[LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

Given two words word1 and word2, find the minimum number of operations required to convert word1to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1 = "ho

[LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming

Given an array of integers nums, write a method that returns the "pivot" index of this array. We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the ind

[LeetCode] 256. Paint House_Easy tag: Dynamic Programming

There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the sam

[LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming

There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent fence posts have the same color. Return the total number of ways you can paint the fence. Note:n and

[LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac" Output: true Example 2: Input: s1 = "aabcc", s2 = "dbbca", s3 =

[LintCode] Coins in a Line III

Coins in a Line III There are n coins in a line. Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins. Could you please decide the first player will

[LintCode] Coins in a Line II

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