Skip to content

Commit 917e4c0

Browse files
committed
Add problem 312 - Burst Balloons
1 parent 7d63d9f commit 917e4c0

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

Diff for: problems/dynamic_programming/burst_balloons.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package dynamic_programming
2+
3+
func BurstBalloons(nums []int) int {
4+
// Array to store balloons
5+
balloons := make([]int, len(nums)+2)
6+
n := len(balloons)
7+
balloons[0] = 1
8+
balloons[n-1] = 1
9+
for i := 1; i < n-1; i++ {
10+
balloons[i] = nums[i-1]
11+
}
12+
// Lookup table
13+
lookup := make([][]int, n-1)
14+
for i := range lookup {
15+
lookup[i] = make([]int, n-1)
16+
for j := range lookup[i] {
17+
lookup[i][j] = -1
18+
}
19+
}
20+
// Burst all balloons between 1 and n - 2
21+
return burst(balloons, 1, n-2, lookup)
22+
}
23+
24+
func burst(balloons []int, i int, j int, lookup [][]int) int {
25+
// Base case
26+
if i > j {
27+
return 0
28+
}
29+
// If there is only one balloon
30+
if i == j {
31+
temp := balloons[i]
32+
if i-1 >= 0 {
33+
temp = temp * balloons[i-1]
34+
}
35+
if j+1 < len(balloons) {
36+
temp = temp * balloons[j+1]
37+
}
38+
return temp
39+
}
40+
// If we have already calculated this result
41+
if lookup[i][j] != -1 {
42+
return lookup[i][j]
43+
}
44+
// Score
45+
score := 0
46+
// Burst all balloons between range i...j
47+
for k := i; k <= j; k++ {
48+
temp := balloons[k]
49+
if i-1 >= 0 {
50+
temp = temp * balloons[i-1]
51+
}
52+
if j+1 < len(balloons) {
53+
temp = temp * balloons[j+1]
54+
}
55+
temp += (burst(balloons, i, k-1, lookup)) + (burst(balloons, k+1, j, lookup))
56+
score = max(score, temp)
57+
}
58+
lookup[i][j] = score
59+
return score
60+
}

Diff for: tests/dynamic_programming/burst_balloons_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dynamic_programming_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/anirudhology/leetcode-go/problems/dynamic_programming"
7+
)
8+
9+
func TestMaxCoinsSingleBalloon(t *testing.T) {
10+
nums := []int{5}
11+
if result := dynamic_programming.BurstBalloons(nums); result != 5 {
12+
t.Errorf("Expected 5, got %d", result)
13+
}
14+
}
15+
16+
func TestMaxCoinsTwoBalloons(t *testing.T) {
17+
nums := []int{3, 1}
18+
if result := dynamic_programming.BurstBalloons(nums); result != 6 {
19+
t.Errorf("Expected 6, got %d", result)
20+
}
21+
}
22+
23+
func TestMaxCoinsMultipleBalloons(t *testing.T) {
24+
nums := []int{3, 1, 5, 8}
25+
if result := dynamic_programming.BurstBalloons(nums); result != 167 {
26+
t.Errorf("Expected 167, got %d", result)
27+
}
28+
}
29+
30+
func TestMaxCoinsNoBalloons(t *testing.T) {
31+
nums := []int{}
32+
if result := dynamic_programming.BurstBalloons(nums); result != 0 {
33+
t.Errorf("Expected 0, got %d", result)
34+
}
35+
}
36+
37+
func TestMaxCoinsLargeCase(t *testing.T) {
38+
nums := []int{1, 2, 3, 4, 5, 6}
39+
if result := dynamic_programming.BurstBalloons(nums); result != 252 {
40+
t.Errorf("Expected 252, got %d", result)
41+
}
42+
}

0 commit comments

Comments
 (0)