Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

312. 戳气球 #79

Open
webVueBlog opened this issue Sep 5, 2022 · 0 comments
Open

312. 戳气球 #79

webVueBlog opened this issue Sep 5, 2022 · 0 comments

Comments

@webVueBlog
Copy link
Owner

312. 戳气球

Description

Difficulty: 困难

Related Topics: 数组, 动态规划

n 个气球,编号为0n - 1,每个气球上都标有一个数字,这些数字存在数组 nums 中。

现在要求你戳破所有的气球。戳破第 i 个气球,你可以获得 nums[i - 1] * nums[i] * nums[i + 1] 枚硬币。 这里的 i - 1i + 1 代表和 i 相邻的两个气球的序号。如果 i - 1i + 1 超出了数组的边界,那么就当它是一个数字为 1 的气球。

求所能获得硬币的最大数量。

示例 1:

输入:nums = [3,1,5,8]
输出:167
解释:
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins =  3*1*5    +   3*5*8   +  1*3*8  + 1*8*1 = 167

示例 2:

输入:nums = [1,5]
输出:10

提示:

  • n == nums.length
  • 1 <= n <= 300
  • 0 <= nums[i] <= 100

Solution

Language: JavaScript

/**
 * @param {number[]} nums
 * @return {number}
 */
/*
[3, 1, 5, 8]
 0, 1, 2, 3

 dp[0][1] + nums[-1] * nums[2] * nums[len] + dp[3][3]
 dp[0][1] = ?
  dp[0][0] =
  dp[1][1] =
 dp[3][3] = ?

 dp[0][3][?] = Math coin

 dp[0][3][k] = dp[0][k] + dp[k][3] + nums[0] * nums[k] * nums[3]
 dp[i][j][k] = dp[i][k] + dp[k][j] + nums[i] * nums[k] * nums[j]
 */
var maxCoins = function(nums) {
    nums = [1, ...nums, 1]
    let len = nums.length
    let dp = new Array(len).fill(0).map(_ => new Array(len).fill(0))
    // 起始位置
    for (let i = len - 3; i >= 0; i--) {
        // 结束位置
        for (let j = i + 2; j < len; j++) {
            for (let k = i + 1; k < j; k++) {
                dp[i][j] = Math.max(
                    dp[i][j],
                    dp[i][k] + dp[k][j] + nums[i] * nums[k] * nums[j]
                )
            }
        }
    }
    return dp[0][len-1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant