Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

Coding · Dynamic programming

Burst balloons for the most coins

Hard · Target 30 minutes · Dynamic programming · Type asked atGoogleAmazon

Balloons in a row carry the numbers nums. Bursting balloon i earns left · nums[i] · right, where left and right are the numbers on its current neighbours (a missing neighbour counts as 1); the burst balloon then disappears and its neighbours become adjacent. Burst them all and return the most coins possible.

Examples

InputOutputWhy
nums = [3,1,5,8]167Burst 1, 5, 3, 8: 15 + 120 + 24 + 8.
nums = [1,5]10Burst 1 (5 coins), then 5 (5 coins).
nums = [7]7One balloon, neighbours count as 1.

Constraints

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

Hints

Hint 1

Pad the row with a 1 at each end. Think about the range strictly between two padded positions i and j.

Hint 2

Choose the balloon k that is burst LAST in that range: at that moment its neighbours are exactly i and j.

Hint 3

coins(i, j) = max over k of coins(i, k) + coins(k, j) + v[i] · v[k] · v[j].

Where people lose marks

  • Choosing the first balloon burst, which leaves the two sides dependent.
  • Forgetting the padding ones.
  • Filling the table by rows instead of by range length.
Console⌘/Ctrl + Enter runs

Write a solution and run it against the real test table.


Keep going

Preparing for a real process? Quant interview preparation, or book a free 20-minute call.