File tree 2 files changed +38
-0
lines changed 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 312. Burst Balloons
3
+ * https://leetcode.com/problems/burst-balloons/
4
+ * Difficulty: Hard
5
+ *
6
+ * You are given n balloons, indexed from 0 to n - 1. Each balloon is painted with a number
7
+ * on it represented by an array nums. You are asked to burst all the balloons.
8
+ *
9
+ * If you burst the ith balloon, you will get nums[i - 1] * nums[i] * nums[i + 1] coins.
10
+ * If i - 1 or i + 1 goes out of bounds of the array, then treat it as if there is a
11
+ * balloon with a 1 painted on it.
12
+ *
13
+ * Return the maximum coins you can collect by bursting the balloons wisely.
14
+ */
15
+
16
+ /**
17
+ * @param {number[] } nums
18
+ * @return {number }
19
+ */
20
+ var maxCoins = function ( nums ) {
21
+ const track = [ 1 , ...nums , 1 ] ;
22
+ const dp = new Array ( nums . length + 2 ) . fill ( ) . map ( ( ) => new Array ( nums . length + 2 ) . fill ( 0 ) ) ;
23
+
24
+ for ( let count = 1 ; count <= nums . length ; count ++ ) {
25
+ for ( let i = 1 ; i <= nums . length - count + 1 ; i ++ ) {
26
+ const j = i + count - 1 ;
27
+ for ( let k = i ; k <= j ; k ++ ) {
28
+ dp [ i - 1 ] [ j + 1 ] = Math . max (
29
+ dp [ i - 1 ] [ j + 1 ] ,
30
+ dp [ i - 1 ] [ k ] + dp [ k ] [ j + 1 ] + track [ i - 1 ] * track [ k ] * track [ j + 1 ]
31
+ ) ;
32
+ }
33
+ }
34
+ }
35
+
36
+ return dp [ 0 ] [ nums . length + 1 ] ;
37
+ } ;
Original file line number Diff line number Diff line change 253
253
307|[ Range Sum Query - Mutable] ( ./0307-range-sum-query-mutable.js ) |Medium|
254
254
309|[ Best Time to Buy and Sell Stock with Cooldown] ( ./0309-best-time-to-buy-and-sell-stock-with-cooldown.js ) |Medium|
255
255
310|[ Minimum Height Trees] ( ./0310-minimum-height-trees.js ) |Medium|
256
+ 312|[ Burst Balloons] ( ./0312-burst-balloons.js ) |Hard|
256
257
315|[ Count of Smaller Numbers After Self] ( ./0315-count-of-smaller-numbers-after-self.js ) |Hard|
257
258
316|[ Remove Duplicate Letters] ( ./0316-remove-duplicate-letters.js ) |Medium|
258
259
318|[ Maximum Product of Word Lengths] ( ./0318-maximum-product-of-word-lengths.js ) |Medium|
You can’t perform that action at this time.
0 commit comments