oneby7

2-D Dynamic Programming Splitting a range

572. Burst Balloons

Hard 147 DSA Sheet

There are n balloons in a row, and balloon i has the number nums[i] on it. You burst the balloons one at a time, in any order you choose, until none are left.

Bursting a balloon gives you coins: its number times the numbers of its two neighbours. A neighbour is the nearest balloon on that side that has not burst yet. When a balloon bursts, the balloons on either side of it become neighbours. If a balloon has no neighbour on one side, that side counts as 1.

Return the most coins you can collect.

Example 1

Input: nums = [4, 2, 3, 5]

Output: 109

Explanation: Burst 2 for 4 × 2 × 3 = 24 coins, then 3 for 4 × 3 × 5 = 60, then 4 for 1 × 4 × 5 = 20, and last 5 for 1 × 5 × 1 = 5. That is 109 coins.

Example 2

Input: nums = [7]

Output: 7

Explanation: The only balloon has no neighbours, so both sides count as 1, and it gives 1 × 7 × 1 = 7.

Example 3

Input: nums = [10000, 10000, 10000]

Output: 1000100010000

Explanation: Burst the middle balloon first, for 10000 × 10000 × 10000 coins. Then one end balloon gives 10000 × 10000, and the last one gives 10000.

Check your understanding

Work out the answer for this input before you write any code.

nums = [2, 1, 7, 6]

Constraints

  • 1 ≤ nums.length ≤ 300
  • 0 ≤ nums[i] ≤ 104

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.