Skip to content

Commit dd79296

Browse files
authored
Merge pull request larissalages#267 from Vedant-S/patch-4
Added Leetcode 312 Burst Balloons solution
2 parents a3ef3cc + 57d1d64 commit dd79296

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def maxCoins(self, iNums):
3+
nums = [1] + [i for i in iNums if i > 0] + [1] Clear digital # 0, since 0 is not scored, and then either end, [1], to facilitate the calculation
4+
n = len(nums)
5+
dp = [[0] * n for _ in range(n)] # Initialize dp
6+
for k in range(2, n): # K determining the size of a sliding window, from start 2
7+
for left in range(0, n - k): # Sliding window, sliding from left to right, determine the start of the interval (left), end (right) position
8+
right = left + k
9+
for i in range(left + 1, right): # Begin enumeration, which number in the range as the last one to be punctured to make it the highest score
10+
dp[left][right] = max(dp[left][right], nums[left] * nums[i] * nums[right] + dp[left][i] + dp[i][right])
11+
return dp[0][n - 1]
12+
13+
14+
if __name__ == '__main__':
15+
nums = [3,1,5,8]
16+
solu = Solution()
17+
out = solu.maxCoins(nums)
18+
print(out)

0 commit comments

Comments
 (0)