返回介绍

solution / 0300-0399 / 0312.Burst Balloons / README_EN

发布于 2024-06-17 01:04:02 字数 4275 浏览 0 评论 0 收藏 0

312. Burst Balloons

中文文档

Description

You are given n balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it represented by an array nums. You are asked to burst all the balloons.

If you burst the ith balloon, you will get nums[i - 1] * nums[i] * nums[i + 1] coins. If i - 1 or i + 1 goes out of bounds of the array, then treat it as if there is a balloon with a 1 painted on it.

Return _the maximum coins you can collect by bursting the balloons wisely_.

 

Example 1:

Input: nums = [3,1,5,8]
Output: 167
Explanation:
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

Example 2:

Input: nums = [1,5]
Output: 10

 

Constraints:

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

Solutions

Solution 1

class Solution:
  def maxCoins(self, nums: List[int]) -> int:
    nums = [1] + nums + [1]
    n = len(nums)
    dp = [[0] * n for _ in range(n)]
    for l in range(2, n):
      for i in range(n - l):
        j = i + l
        for k in range(i + 1, j):
          dp[i][j] = max(
            dp[i][j], dp[i][k] + dp[k][j] + nums[i] * nums[k] * nums[j]
          )
    return dp[0][-1]
class Solution {
  public int maxCoins(int[] nums) {
    int[] vals = new int[nums.length + 2];
    vals[0] = 1;
    vals[vals.length - 1] = 1;
    System.arraycopy(nums, 0, vals, 1, nums.length);
    int n = vals.length;
    int[][] dp = new int[n][n];
    for (int l = 2; l < n; ++l) {
      for (int i = 0; i + l < n; ++i) {
        int j = i + l;
        for (int k = i + 1; k < j; ++k) {
          dp[i][j]
            = Math.max(dp[i][j], dp[i][k] + dp[k][j] + vals[i] * vals[k] * vals[j]);
        }
      }
    }
    return dp[0][n - 1];
  }
}
class Solution {
public:
  int maxCoins(vector<int>& nums) {
    nums.insert(nums.begin(), 1);
    nums.push_back(1);
    int n = nums.size();
    vector<vector<int>> dp(n, vector<int>(n));
    for (int l = 2; l < n; ++l) {
      for (int i = 0; i + l < n; ++i) {
        int j = i + l;
        for (int k = i + 1; k < j; ++k) {
          dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + nums[i] * nums[k] * nums[j]);
        }
      }
    }
    return dp[0][n - 1];
  }
};
func maxCoins(nums []int) int {
  vals := make([]int, len(nums)+2)
  for i := 0; i < len(nums); i++ {
    vals[i+1] = nums[i]
  }
  n := len(vals)
  vals[0], vals[n-1] = 1, 1
  dp := make([][]int, n)
  for i := 0; i < n; i++ {
    dp[i] = make([]int, n)
  }
  for l := 2; l < n; l++ {
    for i := 0; i+l < n; i++ {
      j := i + l
      for k := i + 1; k < j; k++ {
        dp[i][j] = max(dp[i][j], dp[i][k]+dp[k][j]+vals[i]*vals[k]*vals[j])
      }
    }
  }
  return dp[0][n-1]
}
function maxCoins(nums: number[]): number {
  let n = nums.length;
  let dp = Array.from({ length: n + 1 }, v => new Array(n + 2).fill(0));
  nums.unshift(1);
  nums.push(1);
  for (let i = n - 1; i >= 0; --i) {
    for (let j = i + 2; j < n + 2; ++j) {
      for (let k = i + 1; k < j; ++k) {
        dp[i][j] = Math.max(nums[i] * nums[k] * nums[j] + dp[i][k] + dp[k][j], dp[i][j]);
      }
    }
  }
  return dp[0][n + 1];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文