The Indian Engineer

Problem 1671 Minimum Number of Removals to Make Mountain Array

Posted on 7 mins

Array Binary-Search Dynamic-Programming Greedy

Problem Statement

Link - Problem 1671

Question

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

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

Example 1:

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

Example 2:

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:

Solution

class Solution {
public:
    int minimumMountainRemovals(vector<int>& nums) {
        int n = nums.size();
        vector<int> LIS(n, 1), LDS(n, 1);

        // Compute LIS up to each index
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                if (nums[i] > nums[j]) {
                    LIS[i] = max(LIS[i], LIS[j] + 1);
                }
            }
        }

        // Compute LDS from each index
        for (int i = n - 1; i >= 0; --i) {
            for (int j = n - 1; j > i; --j) {
                if (nums[i] > nums[j]) {
                    LDS[i] = max(LDS[i], LDS[j] + 1);
                }
            }
        }

        int maxMountainLength = 0;

        // Find the maximum mountain length
        for (int i = 1; i < n - 1; ++i) {
            if (LIS[i] > 1 && LDS[i] > 1) {  // Valid peak
                maxMountainLength = max(maxMountainLength, LIS[i] + LDS[i] - 1);
            }
        }

        return n - maxMountainLength;
    }
};

Complexity Analysis

| Algorithm           | Time Complexity | Space Complexity |
| ------------------- | --------------- | ---------------- |
| Dynamic Programming | O(n^2)          | O(n)             |

Explanation

Intial Thoughts

To approach this problem, think about how a mountain range forms - it has to increase and then decrease. We need to find the longest sequence of numbers in the array that follows this pattern. It’s also essential to identify the peak point where the sequence starts to decrease. This requires analyzing the array from both ends towards the center. Consider how many elements can be removed to make the array resemble a mountain range.

Intuitive Analysis

Imagine the array as a series of hills and valleys. The goal is to find the largest mountain that can be formed by removing elements. Think of the Longest Increasing Subsequence (LIS) and Longest Decreasing Subsequence (LDS) as tools to analyze the increase and decrease patterns in the array. By combining these patterns, we can identify the peak of the mountain and calculate the minimum number of elements to remove. Consider the trade-offs between removing elements to create a longer mountain range versus preserving the existing patterns.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Hard difficulty.

Hints

Think the opposite direction instead of minimum elements to remove the maximum mountain subsequence

Think of LIS it’s kind of close


Similar Questions:

Title URL Difficulty
Longest Increasing Subsequence https://leetcode.com/problems/longest-increasing-subsequence Medium
Longest Mountain in Array https://leetcode.com/problems/longest-mountain-in-array Medium
Peak Index in a Mountain Array https://leetcode.com/problems/peak-index-in-a-mountain-array Medium
Valid Mountain Array https://leetcode.com/problems/valid-mountain-array Easy
Find in Mountain Array https://leetcode.com/problems/find-in-mountain-array Hard
Beautiful Towers II https://leetcode.com/problems/beautiful-towers-ii Medium
Beautiful Towers I https://leetcode.com/problems/beautiful-towers-i Medium