You are given n balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it represented by an array nums. You are asked to burst all the balloons.

If you burst the ith balloon, you will get nums[i - 1] * nums[i] * nums[i + 1] coins. If i - 1 or i + 1 goes out of bounds of the array, then treat it as if there is a balloon with a 1 painted on it.

Return the maximum coins you can collect by bursting the balloons wisely.

&nbsp;
Example 1:

Input: nums = [3,1,5,8]
Output: 167
Explanation:
nums = [3,1,5,8] --&gt; [3,5,8] --&gt; [3,8] --&gt; [8] --&gt; []
coins =  3*1*5    +   3*5*8   +  1*3*8  + 1*8*1 = 167

Example 2:

Input: nums = [1,5]
Output: 10


&nbsp;
Constraints:


	n == nums.length
	1 &lt;= n &lt;= 300
	0 &lt;= nums[i] &lt;= 100


from functools import cache

class Solution:
    def maxCoins(self, nums: List[int]) -> int:
        nums = [1] + nums + [1]
        length = len(nums)
        dp = [[None]*(length+1) for i in range(length+1)]
        
        @cache
        def dfs(l,r):
            if l>r: return 0
            if dp[l][r] is not None: return dp[l][r]
            dp[l][r] = 0
            for i in range(l,r+1):
                coins = dfs(l, i-1) + dfs(i+1, r) + nums[l-1]*nums[i]*nums[r+1]
                dp[l][r] = max(dp[l][r], coins)
            return dp[l][r]
        return dfs(1, length-2)
class Solution {
    public int maxCoins(int[] nums) {
        int n = nums.length;
        
//         adding 1 to the front and back
        int[] temp = new int[n + 2];  
        temp[0] = 1;
        for(int i = 1; i < temp.length - 1; i++){
            temp[i] = nums[i-1];
        }
        temp[temp.length - 1] = 1;
        nums = temp;
        
//         memoization
        int[][] dp = new int[n+1][n+1];
        for(int[] row : dp){
            Arrays.fill(row, -1);
        }
        
//         result
        return f(1, n, nums, dp);
    }
    
    int f(int i, int j, int[] a, int[][] dp){
        if(i > j) return 0;
        if(dp[i][j] != -1) return dp[i][j];
        
        int max = Integer.MIN_VALUE;
        for(int n = i; n <= j; n++){
            int coins = a[i-1] * a[n] * a[j+1] + f(i, n-1, a, dp) + f(n+1, j, a, dp);
            max = Math.max(max, coins);
        }
        return dp[i][j] = max;
    }
}

// Time Complexity: O(N * N * N) ~ O(N^3);
// Space Complexity: O(N^2) + O(N);
class Solution {
public:
    //topDown+memoization
    int solve(int i,int j,vector<int>& nums,vector<vector<int>>& dp){
        if(i>j) return 0;
        if(dp[i][j]!=-1) return dp[i][j];
        
        int maxcost = INT_MIN;
        for(int k = i;k<=j;k++){
            int cost = nums[i-1]*nums[k]*nums[j+1]+solve(i,k-1,nums,dp)+solve(k+1,j,nums,dp);
            maxcost = max(maxcost,cost);
        }
        return dp[i][j] = maxcost;
    }
    
    int maxCoins(vector<int>& nums) {
        nums.insert(nums.begin(),1);
        nums.push_back(1);
        int n = nums.size();
        vector<vector<int>> dp(n+1,vector<int>(n+1,-1));
        return solve(1,n-2,nums,dp);
    }
    
    //bottomUp dp
    int maxCoins(vector<int>& nums) {
        //including the nums[-1] == 1 and nums[n] == 1
        int n = nums.size();
        nums.insert(nums.begin(),1);
        nums.push_back(1);
        vector<vector<int>> dp(nums.size()+1,vector<int>(nums.size()+1,0));
        
        for (int len = 1; len <= n; ++len)
            for (int left = 1; left <= n - len + 1; ++left) {
                int right = left + len - 1;
                for (int k = left; k <= right; ++k)
                    dp[left][right] = max(dp[left][right], nums[left-1]*nums[k]*nums[right+1] + dp[left][k-1] + dp[k+1][right]);
            }
        return dp[1][n];
    }
};
var rec = function(i,j,arr,dp){
    if(i>j)return 0;
    if(dp[i][j] !== -1)return dp[i][j];
    let max = Number.MIN_VALUE;
    for(let k=i;k<=j;k++){
        let cost = arr[i-1] * arr[k] * arr[j+1] + rec(i,k-1,arr,dp)+rec(k+1,j,arr,dp);
        if(cost>max){
            max = cost
        }
    }
    return dp[i][j] = max
}
var maxCoins = function(nums) {
    let n = nums.length;
    let sol = [];
    for(let i=0;i<n+1;i++){
        sol[i] = Array(n+1).fill(-1)
    }
    nums.unshift(1);
    nums.push(1)
    return rec(1,n,nums,sol)
};
Burst Balloons