class Solution:
def maxCoins(self, nums: List[int]) -> int:
nums_extra = [1] + nums + [1]
size = len(nums_extra)
dp = [[0] * size for _ in range(size)]
for len_of_sec in range(2, size): # enumerate different length of section
for start in range(0, size - len_of_sec): # enumerate the start of section
end = start + len_of_sec
max_coins = float("-inf")
for k in range(start + 1, end): # in the section, caculate the max coins obtained
left_coin, right_coin = dp[start][k], dp[k][end]
cur = nums_extra[start] * nums_extra[k] * nums_extra[end]
total = left_coin + right_coin + cur
max_coins = max(max_coins, total)
dp[start][end] = max_coins
return dp[0][-1]
刷题记录:312. 戳气球
于 2022-01-22 20:25:03 首次发布