[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 is odd, so there are no ties.

Alex and Lee take turns, with Alex starting first.  Each turn, a player takes the entire pile of stones from either the beginning or the end of the row.  This continues until there are no more piles left, at which point the person with the most stones wins.

Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.

Example 1:

Input: [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.

Note:

  1. 2 <= piles.length <= 500
  2. piles.length is even.
  3. 1 <= piles[i] <= 500
  4. sum(piles) is odd.

这个题目思路跟[LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈很像, 只不过这里是利用 区间Dynamic Programing的方法,所以只用一维的dp已经不够了, 另外初始化的时候我们不直接用for loop, 而是用类似于dfs recursive的方法去将初始化放在helper fuction里面. 另外得到的

动态方程式为  A[i][j] = max( piles[i] + min(A[i+1][j-1] + A[i+2][j]) , piles[j] + min(A[i+1][j-1], A[i][j-2]) )

init;

A[i][i] = piles[i]

A[i][i+1] = max(piles[i], piles[i+1])

1. Constraints

1) size [2,500], even number

2) element [1,50], integer

3) sum(piles) is odd, no ties

2. Ideas

Dynamic Programming   ,     T: O(n^2)     S; O(n^2)

3. Code

 1 class Solution:
 2     def stoneGame(self, piles):
 3         n = len(piles)
 4         dp, flag = [[0]*n for _ in range(n)], [[0]*n for _ in range(n)]
 5         def helper(left, right):
 6             if flag[left][right]:
 7                 return dp[left][right]
 8             if left == right:
 9                 dp[left][right] = piles[left]
10             elif left + 1 = right:
11                 dp[left][right] = max(piles[left], piles[right])
12             elif left < right: # left > right, init 0
13                 value_l = piles[left] + min(helper(left+2, right), helper(left + 1, right -1))
14                 value_r = piles[right] + min(helper(left+1, right-1), helper(left, right - 2))
15                 dp[left][right] = max(value_l, value_r)
16             flag[left][right] = 1
17             return dp[left][right]
18         return helper(0, n-1) > sum(piles)//2

4. Test cases

[5,3,4,5]

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

时间: 2024-12-24 04:12:32

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

[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

[LeetCode] questions for Dynamic Programming

Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic P

[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 1140. Stone Game II

原题链接在这里:https://leetcode.com/problems/stone-game-ii/ 题目: Alex and Lee continue their games with piles of stones.  There are a number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].  The objective of the gam

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

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 play will win or lose? Have you met this questi

[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

LeetCode Coins in a Line

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 play will win or lose? 该题类似于下题: 箱子里面有一百个球,甲和乙分别

Coins in a Line

ref: http://www.lintcode.com/en/problem/coins-in-a-line/ 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