LeetCode #312: Burst Balloons

原创 2016年08月30日 12:17:24

Problem Statement

(Source) 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:
(1) You may imagine nums[-1] = nums[n] = 1. They are not real therefore you can not burst them.
(2) 0 ≤ n ≤ 500, 0 ≤ nums[i] ≤ 100

Example:

Given [3, 1, 5, 8]

Return 167

   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

Analysis

This problem has an obvious indication that we can use Dynamic Programming approach to solve it. After making a decision on using dynamic programming, the next step is to find the recurrence structure of how global optimal solution can be formed by optimal solutions of sub-problems.

Let dp[i][j] denotes the optimal solution for the array slice nums[i : j + 1]. Put it in another way, dp[i][j] means the number of coins we can get when we only burst balloons indexed from i to j. At this point, we get stuck, so let us try from small input size. What if the input array is of size 1? The solution would be the number on this single balloon. Then think about what if the input array if of size 2? In this case, what matters is the last balloon we burst, right? Because the coins we get by our first bursting would be the same no matter which ballon we choose to burst first, and we have to burst twice. Now, we may formulate our Dynamic Programming Recurrence:

# k is the last balloon we choose to burst in range i...j.
for k in i...j:     
    dp[i][j] = max(dp[i][j], nums[i - 1] * nums[k] * nums[j + 1] + dp[i][k - 1] + dp[k + 1][j])

Solution

class Solution(object):
    def maxCoins(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums:
            return 0

        n = len(nums)
        nums.insert(0, 1)
        nums.append(1)
        dp = [[0 for i in xrange(n + 2)] for j in xrange(n + 2)]
        dp[0][0] = 1
        dp[n + 1][n + 1] = 1
        for length in xrange(1, n + 1):
            for i in xrange(1, n - length + 2):
                j = i + length - 1
                for k in xrange(i, j + 1):
                    dp[i][j] = max(dp[i][j], nums[i - 1] * nums[k] * nums[j + 1] + dp[i][k - 1] + dp[k + 1][j])

        return dp[1][n]
版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode 312. Burst Balloons

题目312. Burst Balloons思路考虑最后一个戳破的气球,这个气球的位置可以把整个气球数组分成两部分。 注意是最后一个,不是第一个,之前一直没转过弯来。 利用动态规划思路:动态规划数组...
  • zly9923218
  • zly9923218
  • 2016年04月05日 00:03
  • 1861

leetcode 312. Burst Balloons 分析

最近在学习动态规划,因为之前初步看了CLRS上面的动态规划部分,觉得已经有了一定的了解,于是想去leetcode上面刷一下动归部分的题目。说实话,不简单。主要还是自己对动态规划没有理解好。按accep...
  • Swartz2015
  • Swartz2015
  • 2016年01月22日 14:39
  • 4996

LeetCode 312. Burst Balloons(戳气球)

原题网址:https://leetcode.com/problems/burst-balloons/ Given n balloons, indexed from 0 to n-1. Each ...
  • jmspan
  • jmspan
  • 2016年04月21日 14:39
  • 1432

leetcode -- Burst Balloons -- 重点dp

https://leetcode.com/problems/burst-balloons/类似于矩阵连乘的问题,但与house robber问题不一样。这里是2D dp,决策变量是在那个位置burst...
  • xyqzki
  • xyqzki
  • 2015年12月28日 12:50
  • 3668

【leetcode题解】【再来,动态规划】【H】【21.8】Burst Balloons

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented b...
  • sscssz
  • sscssz
  • 2015年12月21日 19:19
  • 577

leetcode 312 : Burst Balloons

1、原题如下: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it repre...
  • l3368bcttqnqn
  • l3368bcttqnqn
  • 2015年12月05日 20:15
  • 496

Leetcode 312: Burst Balloons

Leetcode 312: Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is painted with...
  • u010045971
  • u010045971
  • 2017年08月20日 10:01
  • 260

leetcode 312 Burst Balloons

题意: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represente...
  • shiyang6017
  • shiyang6017
  • 2015年12月04日 14:04
  • 251

LeetCode 312 - Burst Balloons

LeetCode 312 - Burst Balloons题目 Given n balloons, indexed from 0 to n-1. Each balloon is painted w...
  • Gregzeng
  • Gregzeng
  • 2016年09月11日 17:19
  • 169

leetcode Burst Balloons

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented b...
  • murmured
  • murmured
  • 2015年11月30日 14:52
  • 2539
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode #312: Burst Balloons
举报原因:
原因补充:

(最多只允许输入30个字)