Skip to content

Instantly share code, notes, and snippets.

@jan25
Created November 20, 2018 20:33
Show Gist options
  • Save jan25/5029e21cccfe3d9e1b8781645f100ccb to your computer and use it in GitHub Desktop.
Save jan25/5029e21cccfe3d9e1b8781645f100ccb to your computer and use it in GitHub Desktop.
'''
https://leetcode.com/problems/burst-balloons/description/
'''
class Solution:
def maxCoins(self, nums):
nums = [1, *nums, 1]
return self.dp(0, len(nums) - 1, nums, {})
def dp(self, a, b, nums, memo):
if abs(a - b) < 2: return 0
if (a, b) in memo: return memo[(a, b)]
sub_sol, side_prod = 0, nums[a] * nums[b]
for i in range(a + 1, b):
sub_sol = max(sub_sol, nums[i] * side_prod + self.dp(a, i, nums, memo) + self.dp(i, b, nums, memo))
memo[(a, b)] = sub_sol
return sub_sol
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment