1188. Minimum Number of Removals to Make Mountain Array

MediumDynamic ProgrammingLIS Family

An array is a mountain if there is an index i (not at either end) such that values strictly increase up to i and strictly decrease after it. Given nums, return the minimum number of elements to remove so the remaining array is a mountain. The input is JSON {nums}.

Input: JSON {nums}.

Output: Integer — the minimum removals.

Examples

Example 1
Input: {"nums":[1,3,1]}
Output: 0
Explanation: Already a mountain.
Example 2
Input: {"nums":[2,1,1,5,6,2,3,1]}
Output: 3
Explanation: Remove three to form a mountain.
Example 3
Input: {"nums":[9,8,1,7,6,5,4,3,2,1]}
Output: 2
Explanation: Trim to a valid mountain.

Constraints

Asked by

MicrosoftBloombergAmazonGoogleMeta
Solve this problem in the editor →