There’s a question
Given an array ARR
In each turn (from 1 to N , until array is deleted),
you can either Delete 1st element or Last element of that array and multiply it by turn number and ADD the max element present in the array.
You keep doing it until array id completely deleted and Print the total number
For example
Arr = 5 4 3 6 2
Answer = 96
Explanation
2 * 1 + 6 = 8
5 * 2 + 6 = 16
3 * 4 + 6 = 18
4 * 3 + 6 = 18
6 * 5 + 6 = 36
I did the solution by using Dynamic Programming Top Down approach as
int topDown(int i,int j,int a[],int turn){
if(i>j) return 0;
if(dp[i][j]!=-1) return dp[i][j];
int x = ((a[i]*turn) + RMQ(i,j)) + topDown(i+1,j,a,turn+1);
int y = ((a[j]*turn) + RMQ(i,j)) + topDown(i,j-1,a,turn+1);
return dp[i][j] = max(x,y);
}
int ans = topDown(0,n-1,ARR,1);
RMQ is Range Max Query with a Sparse Table of ARR O(1) TC
Can anyone Help me finding a Bottom UP approach to this solution?