Skip to content

Commit 99c7b55

Browse files
authored
Create 312-Burst-Balloons.py
1 parent ba3d012 commit 99c7b55

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

312-Burst-Balloons.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def maxCoins(self, nums: List[int]) -> int:
3+
cache = {}
4+
nums = [1] + nums + [1]
5+
6+
for offset in range(2, len(nums)):
7+
for left in range(len(nums) - offset):
8+
right = left + offset
9+
for pivot in range(left + 1, right):
10+
coins = nums[left] * nums[pivot] * nums[right]
11+
coins += cache.get((left, pivot), 0) + cache.get((pivot, right), 0)
12+
cache[(left, right)] = max(coins, cache.get((left, right), 0))
13+
return cache.get((0, len(nums) - 1), 0)

0 commit comments

Comments
 (0)