<p>You may recall that an array <code>arr</code> is a <strong>mountain array</strong> if and only if:</p>

<ul>
	<li><code>arr.length &gt;= 3</code></li>
	<li>There exists some index <code>i</code> (<strong>0-indexed</strong>) with <code>0 &lt; i &lt; arr.length - 1</code> such that:
	<ul>
		<li><code>arr[0] &lt; arr[1] &lt; ... &lt; arr[i - 1] &lt; arr[i]</code></li>
		<li><code>arr[i] &gt; arr[i + 1] &gt; ... &gt; arr[arr.length - 1]</code></li>
	</ul>
	</li>
</ul>

<p>Given an integer array <code>nums</code>​​​, return <em>the <strong>minimum</strong> number of elements to remove to make </em><code>nums<em>​​​</em></code><em> </em><em>a <strong>mountain array</strong>.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The array itself is a mountain array so we do not need to remove any elements.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,1,1,5,6,2,3,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> One solution is to remove the elements at indices 0, 1, and 5, making the array nums = [1,5,6,3,1].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li>It is guaranteed that you can make a mountain array out of <code>nums</code>.</li>
</ul>

class Solution {
public:
    int t[1001][2][1002][2];
    int f(int i, int state, int prev,vector<int>& nums, int cnt){
        int n= nums.size();
        if(i>=n && state==1){
            return 0;
        }
        else if(i>=n && state==0) {
             return -1e9;
        }
        if(t[i][state][prev+1][cnt]!=-1){
            return t[i][state][prev+1][cnt];
        }
        int notake= 0+f(i+1,state,prev,nums, cnt);
        int take= -1e9;
        if(state==0){
            if(prev==-1 ){
                take= 1+f(i+1,state,i, nums,cnt);
            }
            else if(nums[prev]<nums[i]){
                if(cnt<1){
                    take= 1+f(i+1,state,i, nums, cnt+1);
                }
                else{
                     take= 1+f(i+1,state,i, nums, cnt);
                }
                
            }
            else if(prev!=-1 && nums[prev]>nums[i]){
               /* int k=0;
                if(prev-1>=0){
                   for(int j=0; j<prev; j++){
                    if(nums[j]<nums[prev]){
                        k=1;
                    }
                   }
                } */
                if(cnt==1){
                    take= 1+f(i+1,1,i, nums, cnt);
                }
                
            }
        }
        else{
            if(nums[prev]>nums[i]){
                take= 1+f(i+1,state,i, nums, cnt);
            }
        }
        return t[i][state][prev+1][cnt]=max(take,notake);
    }

    int minimumMountainRemovals(vector<int>& nums) {
        int n= nums.size();
        memset(t,-1,sizeof(t));
        return n-f(0,0,-1, nums,0);

    }
};
32000