← Microsoft bank

Microsoft · Question 69 · Dynamic Programming

Burst Balloons

Dynamic ProgrammingJavacode⏱ O(n³)💾 O(n²)
Product-based

Problem

Given an array nums, burst balloons to maximize coins. You get nums[i-1] * nums[i] * nums[i+1] for bursting i.

Approach

Interval DP: try each balloon as the last burst in its range.

Time O(n³)Space O(n²)

How it works

  1. 1Pad with virtual 1-balloons.
  2. 2dp[left][right] = best coins strictly inside.
  3. 3Try each balloon as the last burst in the interval.

Sample input / output

Input:
4
3 1 5 8
Output:
167

Constraints

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

Solution — read it, then walk it

Time O(n³)Space O(n²)Loops 5Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step.
↻ repeat↻ repeat↻ repeatSTARTimport java.util.*DEFINE class MainDEFINE static intmaxCoins(int[] nums)DEFINE n = nums.lengthDEFINE [] newNums = newint[n + 2]newNums[0] = newNums[n + 1]= 1for (int i = 0; i < n;i++) newNums[i + 1] =nums[i]DEFINE [][] dp = new int[n+ 2][n + 2]for (int len = 2; len<= n + 1; len++)for (int left = 0;left <= n + 1 - len;left++)DEFINE right = left + lenfor (int k = left + 1;k < right; k++)dp[left][right] =Math.max(dp[left][right],RETURN dp[0][n + 1]DEFINE static voidmain(String[] args)java.util.Scanner sc = newjava.util.Scanner(System.in)DEFINE n = sc.nextInt()DEFINE [] nums = new int[n]for (int i = 0; i <nums.length; i++)nums[i] = sc.nextInt()System.out.println(maxCoins(nums))END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/21

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits