Maximise the coins by popping balloons

Given N balloons, indexed from 0 to N - 1. Each balloon is painted with a number on it represented by array A.

You are asked to burst all the balloons. If you burst the balloon i you will get A[left] x A[i] x A[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

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

NOTE:

  • You may imagine A[-1] = A[N] = 1. They are not real. Therefore, you can not burst them.

Input Format:

First and only argument of input contains an integer array A of size N.

Output Format:

Return a single integer representing the maximum coins.

Constraints

1 ≤ N ≤ 500
0 ≤ A[i] ≤ 100

For Example:

Input 1:
    A = [2,3]
Output 1:
    9
Explanation 1:
    it is better to burst 1 followed by 2 for a total of 9 coins.

Input 2:
    A = [2,4,3,2] 
Output 2:
    46
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Maximise the coins by popping balloons on Interview Code Editor
Sign Up
to access hints and editorial solutions for Maximise the coins by popping balloons

Discussion


Loading...
Click here to start solving coding interview questions