<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:
    def minimumMountainRemovals(self, nums: List[int]) -> int:
        """
        - idea: there will be an index i around which mountain is formed
        - increasing[i] <- min number of elements to remove before i to make nums increasing up-to i
            + or longest increasing subsequence ending at i
        - decreasing[i] <- min number of elements to remove after i to make nums decreasing starting i
            + or longest decreasing subsequence starting at i
        - result min over {increasing[i]+decreasing[i]}

        - [1, 3, 5, 10, 11, 7, 12] <- longest increasing subsequence ending at last index
        - [1, 3, 5, 10, 11, 7, 8, 9, 12]
        """

        increasing = [1]*len(nums)
        decreasing = [1]*len(nums)

        for i in range(1, len(nums)):
            result = 1
            for j in range(i):
                if nums[j]<nums[i]:
                    result = max(result, increasing[j]+1)
            increasing[i] = result

        for i in range(len(nums)-2, -1, -1):
            result = 1
            for j in range(i+1, len(nums)):
                if nums[i]>nums[j]:
                    result = max(result, decreasing[j]+1)
            decreasing[i] = result

        # print(f'increasing: {increasing}')
        # print(f'decreasing: {decreasing}')

        maxMountainSub = 3
        for i in range(1, len(nums)-1):
            if (increasing[i]>1) and (decreasing[i]>1):
                maxMountainSub = max(maxMountainSub, increasing[i]+decreasing[i]-1)

        return len(nums)-maxMountainSub
# TC: O(N^2)
# SC: O(N)
16800