> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Number of Removals to Make Mountain

> Tested Python solution for LeetCode 1671 with 23 pytest cases. Generate a practice environment with lcpy.

LeetCode 1671, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Dynamic Programming](/catalog/topics/dynamic-programming), [Greedy](/catalog/topics/greedy). [View on LeetCode](https://leetcode.com/problems/minimum-number-of-removals-to-make-mountain-array/description/).

Generate this problem as a practice environment: tested reference solution, 23 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1671   # by problem number
lcpy gen -s minimum_number_of_removals_to_make_mountain_array   # by problem name
```

## Problem

You may recall that an array `arr` is a **mountain array** if and only if:

* `arr.length >= 3`
* There exists some index `i` (**0-indexed**) with `0 < i < arr.length - 1` such that:
  * `arr[0] < arr[1] < ... < arr[i - 1] < arr[i]`
  * `arr[i] > arr[i + 1] > ... > arr[arr.length - 1]`

Given an integer array `nums`, return the minimum number of elements to remove to make `nums` a mountain array.

### Examples

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

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

### Constraints

* `3 <= nums.length <= 1000`
* `1 <= nums[i] <= 10^9`
* It is guaranteed that you can make a mountain array out of `nums`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_removals_to_make_mountain_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_number_of_removals_to_make_mountain_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from bisect import bisect_left


class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def minimum_mountain_removals(self, nums: list[int]) -> int:
        def lis_lengths(seq: list[int]) -> list[int]:
            tails: list[int] = []
            lengths: list[int] = []
            for x in seq:
                pos = bisect_left(tails, x)
                if pos == len(tails):
                    tails.append(x)
                else:
                    tails[pos] = x
                lengths.append(pos + 1)
            return lengths

        inc = lis_lengths(nums)
        dec = lis_lengths(nums[::-1])[::-1]

        best = 0
        for i in range(1, len(nums) - 1):
            if inc[i] >= 2 and dec[i] >= 2:
                best = max(best, inc[i] + dec[i] - 1)
        return len(nums) - best
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.