Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Burst Balloons

Posted on August 5, 2019July 26, 2020 by braindenny

Burst Balloons



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #dynamicprogramming, #intervaldp

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

Find the maximum coins you can collect by bursting the balloons wisely.

Note:

  • You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
  • 0 <= n <= 500, 0 <= nums[i] <= 100

Example:

Input: [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

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/burst-balloons
// Basic Ideas: DP over Interval
//
//   dp(i, j) = max(dp(i, k)+dp(k+1,j)+burst k)
//           Treat k as the last one to burst
//
// Complexity: Time O(n^3), Space O(n^2)
func max(x, y int) int {
    if x>y {
        return x
    } else {
        return y
    }
}

func maxCoins(nums []int) int {
    if len(nums) == 0 {
        return 0
    }
    nums2 := make([]int, len(nums)+2)
    nums2[0] = 1
    nums2[len(nums2)-1] = 1
    for i:=1; i<len(nums2)-1; i++ {
        nums2[i] = nums[i-1]
    }
    dp := make([][]int, len(nums))
    for i, _ := range dp {
        dp[i] = make([]int, len(nums))
        // base condition. Length: 1
        dp[i][i] = nums2[i]*nums2[i+1]*nums2[i+2]
    }
    for i:=len(nums)-1; i>=0; i-- {
        for j:=i+1; j<len(nums); j++ {
            dp[i][j] = 0
            for k:=i; k<=j; k++ {
                v1, v2 := 0, 0
                if k-1>=i {
                    v1 = dp[i][k-1]
                }
                if j>=k+1 {
                    v2 = dp[k+1][j]
                }
                dp[i][j] = max(dp[i][j], v1+v2+nums2[k+1]*nums2[i]*nums2[j+2])
            }
        }
    }
    return dp[0][len(nums)-1]
}
linkedin
github
slack

Post Views: 0
Posted in MediumTagged #dynamicprogramming, intervaldp

Post navigation

LeetCode: Strange Printer
LeetCode: The Dining Philosophers

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.