150强访谈中等

气球爆裂

“气球爆裂”问题的详细指南和 Python 实现。

问题陈述

中等

给你 n 个气球,索引从 0 到 n - 1。每个气球上都画有一个数字,用数组 nums 表示。你被要求爆破所有的气球。

如果你戳破第 i 个气球,你将获得 nums[i - 1] * nums[i] * nums[i + 1] 个硬币。如果 i - 1 或 i + 1 超出了数组的范围,则将其视为一个气球,上面画有 1。

通过明智地爆破气球,返还您可以收集到的最大硬币。

编写一个函数 maxCoins(nums: List[int]) -> int

约束条件
  • n == len(nums)
  • 1 <= n <= 300
  • 0 <= nums[i] <= 100

示例

Example 1
Input
nums = [3,1,5,8]
Output
167
Explanation

burst 1 -> burst 5 -> burst 3 -> burst 8. Coins: 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167.

Example 2
Input
nums = [1,5]
Output
10
Explanation

burst 1 first: 1*5*1 = 5, then burst 5: 1*5*1 = 5. Total = 10.

Need a Hint?
考虑使用 2D DP 特定的数据结构,例如集合或堆。
Edge Cases to Watch
  • 空输入结构
  • 单元素输入
  • 大数值范围

准备好解决了吗?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在编辑器中打开
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推荐的 Python 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。