# Bursting Balloons

This is a Leetcode problem -

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 you burst the 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 become 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]   -->    --> []
coins =   3*1*5     +  3*5*8    +   1*3*8     + 1*8*1   = 167


Here is my solution to this challenge -

# Uses dynamic programming

def max_coins(nums):
"""
:type nums: List[int]
:rtype: int
"""
nums =  + nums + 
n = len(nums)
dp = [ * n for i in range(n)]
for j in range(2, n):
for i in range(j - 2, -1, -1):
for k in range(i + 1,j):
dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + nums[i] * nums[j] * nums[k])
return dp[-1]


Here is the Leetcode result (70 test cases) - So, I would like to know whether I could make this program shorter and more efficient.

• Is code length your primary concern? If I see that code, eliminating the doubly nested for-loop would be mine. – Mast Jun 3 at 12:36
• Well, my main concern is efficiency and it would just be better if it could be made shorter. – Justin Jun 3 at 12:58
• Small things dp = [ * n for i in range(n)] can be dp = [ * n for _ in range(n)]. By convention in python if we dont use the index variable we just use _ – Mitchel Paulin Jun 5 at 14:34
{"ft_lang_label":"__label__en","ft_lang_prob":0.72257525,"math_prob":0.99884766,"size":1448,"snap":"2019-35-2019-39","text_gpt3_token_len":494,"char_repetition_ratio":0.11218837,"word_repetition_ratio":0.041044775,"special_character_ratio":0.40055248,"punctuation_ratio":0.13607594,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99841887,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-20T02:30:11Z\",\"WARC-Record-ID\":\"<urn:uuid:7dfeae4d-27c8-4281-8694-b301e8aad61c>\",\"Content-Length\":\"137764\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1c20d8d2-da4f-442b-92d5-5d34f8334b7c>\",\"WARC-Concurrent-To\":\"<urn:uuid:cf3bf961-a815-4b66-90d4-0d9ff74dc886>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://codereview.stackexchange.com/questions/221587/bursting-balloons\",\"WARC-Payload-Digest\":\"sha1:Q4B2IX2J4MZK2GVED5CCOBBWRLGOV5YK\",\"WARC-Block-Digest\":\"sha1:FQNZWVUHT3RH364SVWCYTXSGDMJZHN53\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514573801.14_warc_CC-MAIN-20190920005656-20190920031656-00063.warc.gz\"}"}