<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>

template <class T>
class NodeBST {
    T val;
    int idx;
    int left_cnt = 0, right_cnt = 0;
    NodeBST *left = nullptr, *right = nullptr;
public:
    NodeBST(T val, int idx) : val(val), idx(idx) {};
    ~NodeBST() { delete left; delete right; };
    void insert(T v, int i, NodeBST **parent)
    {
        if(v == val)
        {
            idx = i;
        }
        else if(v < val)
        {
            ++left_cnt;
            if(!left)
            {
                left = new NodeBST(v, i);
            }
            else
            {
                left->insert(v, i, &left);
            }
        }
        else
        {
            ++right_cnt;
            if(!right)
            {
                right = new NodeBST(v, i);
            }
            else
            {
                right->insert(v, i, &right);
            }
        }
        if(left_cnt + 2 < right_cnt)
        {
            *parent = right;
            right = right->left;
            (*parent)->left = this;
            right_cnt = (*parent)->left_cnt;
            (*parent)->left_cnt += left_cnt + 1;
        }
        else if(left_cnt >  right_cnt + 2)
        {
            *parent = left;
            left = left->right;
            (*parent)->right = this;
            left_cnt = (*parent)->right_cnt;
            (*parent)->right_cnt += right_cnt + 1;
        }
    }
    int search_smaller(T target, int *dp)
    {
        int right_res = 0, left_res = 0;
        if(val < target && right)
        {
            right_res = right->search_smaller(target, dp);

        }
        if(left)
        {
            left_res = left->search_smaller(target, dp);
        }
        return max(val < target ? dp[idx] : 0, max(left_res, right_res));

    }
};

class Solution {
public:
    int minimumMountainRemovals(vector<int>& nums) {
        int LIS[1000] = {}, LDS[1000] = {};
        LIS[0] = 1;
        auto root = new NodeBST<int>(nums[0], 0);
        for(int i = 1; i < nums.size(); ++i)
        {
            LIS[i] = root->search_smaller(nums[i], LIS) + 1;
            root->insert(nums[i], i, &root);
        }
        delete root;
        root = new NodeBST<int>(nums.back(), nums.size()-1);
        LDS[nums.size()-1] = 1;
        for(int i = nums.size()-2; i >= 0; --i)
        {
            LDS[i] = root->search_smaller(nums[i], LDS) + 1;
            root->insert(nums[i], i, &root);
        }
        int max_mountain_len = 3;
        for(int i = 0; i < nums.size(); ++i)
        {
            // cout << i << ": " << LIS[i] << " " << LDS[i] << endl;
            if(LIS[i] == 1 || LDS[i] == 1)
            {
                continue;
            }
            max_mountain_len = max(max_mountain_len, LIS[i] + LDS[i]);
        }
        return nums.size() - max_mountain_len + 1;
    }
};
24500