I need to solve this problem with DP and here is the problem: we have an array and we want to make a ascending sub array with maximum size with 2 conditions:
for .e.g:
input : arr[ ] = {0 , 3 , 10 , 7 , 6 , 5 , 14}
output : 5
and the Sub array is {5 , 6, , 7 , 10 , 14}
The solution for this instance is, start with 10 and then put 7 in the left and 6 and 5 to the left then put 14 in the right of 10
It means we can extend the array by this conditions from left and right
Well this is a classical dp problem, quite simple to do with top-down approach.
let's define our state dp[c][dec][inc] - we are now looking at element at position c, the element at the back of the sequence we built is at position dec, and the element at the front of the sequence is at position inc.
So now we can traverse to:
sample code (C++) :
const int n = 7;
int a[] = {0, 0, 3, 10, 7, 6, 5, 14};
int dp[n+1][n+1][n+1];
int solve(int c, int dec, int inc)
{
if(c > n)return 0;
if(dp[c][dec][inc] != -1)return dp[c][dec][inc];
dp[c][dec][inc] = solve(c+1,dec,inc); //skip element
if(inc==0 && dec==0) //if our sequence is empty, try appending element to sequence
dp[c][dec][inc] = max(dp[c][dec][inc], 1 + solve(c+1,c,c));
else if(a[c] >= a[inc])//we can extend our array [dec, ..., inc] because current element can be put to front
dp[c][dec][inc] = max(dp[c][dec][inc], 1 + solve(c+1,dec,c));
else if(a[c] <= a[dec])//we can extend our array [dec, ..., inc] because current element can be put to back
dp[c][dec][inc] = max(dp[c][dec][inc], 1 + solve(c+1,c,inc));
return dp[c][dec][inc];
}
int main()
{
memset(dp, -1, sizeof dp);
cout<<solve(1,0,0);
}
Complexity O(n^3)