Minimum Number of Removals to Make Mountain Array - Problem
Mountain Array Transformation Challenge
Imagine you have an array of integers that you need to transform into a mountain array by removing the minimum number of elements. A mountain array is a special sequence that:
• Has at least 3 elements
• Strictly increases to a peak, then strictly decreases
• The peak cannot be at the first or last position
For example:
Your Goal: Given an integer array
Key Insight: This is essentially finding the longest mountain subsequence, then subtracting its length from the total array length!
Imagine you have an array of integers that you need to transform into a mountain array by removing the minimum number of elements. A mountain array is a special sequence that:
• Has at least 3 elements
• Strictly increases to a peak, then strictly decreases
• The peak cannot be at the first or last position
For example:
[1, 3, 5, 4, 2] is a mountain (increases to 5, then decreases)Your Goal: Given an integer array
nums, find the minimum number of elements to remove to make it a valid mountain array.Key Insight: This is essentially finding the longest mountain subsequence, then subtracting its length from the total array length!
Input & Output
example_1.py — Basic Mountain
$
Input:
[1,3,1]
›
Output:
0
💡 Note:
The array [1,3,1] is already a perfect mountain - it increases to peak 3, then decreases. No removals needed!
example_2.py — Need to Remove Elements
$
Input:
[2,1,1,5,6,2,3,1]
›
Output:
3
💡 Note:
We can form mountain [1,5,6,2,1] by removing elements at positions 0,2,6. The mountain increases from 1→5→6, then decreases 6→2→1.
example_3.py — Minimum Length Case
$
Input:
[4,3,2,1,1,2,3,1]
›
Output:
4
💡 Note:
Best mountain is [1,2,3,1] with length 4, so we remove 8-4=4 elements. The mountain goes 1→2→3→1.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Climbing Potential
For each position, calculate how long an increasing sequence can be that ends there
2
Identify Descending Potential
For each position, calculate how long a decreasing sequence can be that starts there
3
Find the Best Peak
Combine both calculations to find positions that can serve as mountain peaks
4
Calculate Removals
Subtract the longest mountain length from the total array length
Key Takeaway
🎯 Key Insight: Transform the problem into finding the longest mountain subsequence using LIS algorithms, then subtract from total length to get minimum removals.
Time & Space Complexity
Time Complexity
O(2^n * n)
2^n subsequences to generate, each taking O(n) time to validate
⚠ Quadratic Growth
Space Complexity
O(n)
Space for storing current subsequence and recursion stack
⚡ Linearithmic Space
Constraints
- 3 ≤ nums.length ≤ 1000
- 1 ≤ nums[i] ≤ 109
- All elements in the final mountain must be strictly increasing then strictly decreasing
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code