Minimum Number of Removals to Make Mountain Array - Complete Solution Guide
Minimum Number of Removals to Make Mountain Array is LeetCode problem 1671, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 . 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 el
Detailed Explanation
The problem asks us to find the minimum number of elements to remove from a given integer array `nums` to make it a 'mountain array'. A mountain array has the following properties: its length is at least 3, and there exists an index `i` (0-indexed) such that the elements from index 0 up to `i` are strictly increasing, and the elements from index `i` to the end are strictly decreasing. In essence, there's a peak, and the array increases to the peak and then decreases. The goal is to find the longest possible mountain subarray by removing the minimum number of elements from the original array.
Solution Approach
The solution uses dynamic programming to find the longest increasing subsequence (LIS) ending at each index and the longest decreasing subsequence (LDS) starting at each index. For each index `i`, the sum of `LIS[i]` and `LDS[i]` gives the length of a potential mountain with peak at `i`. We then iterate through all possible peak indices and find the maximum possible length of a mountain subarray. Finally, we subtract the maximum mountain length from the original array length to find the minimum number of removals.
Step-by-Step Algorithm
- Step 1: Initialize two arrays, `lis` and `lds`, of the same size as `nums`, filled with 1s. `lis[i]` will store the length of the longest increasing subsequence ending at index `i`, and `lds[i]` will store the length of the longest decreasing subsequence starting at index `i`.
- Step 2: Calculate the `lis` array. Iterate through `nums` from left to right. For each index `i`, iterate through all indices `j` from 0 to `i-1`. If `nums[i] > nums[j]`, update `lis[i]` to be the maximum of its current value and `lis[j] + 1`.
- Step 3: Calculate the `lds` array. Iterate through `nums` from right to left. For each index `i`, iterate through all indices `j` from `n-1` down to `i+1`. If `nums[i] > nums[j]`, update `lds[i]` to be the maximum of its current value and `lds[j] + 1`.
- Step 4: Find the maximum length of a mountain subarray. Iterate through `nums` from index 1 to `n-2`. For each index `i`, check if `lis[i] > 1` and `lds[i] > 1` (ensuring there's an increasing part before the peak and a decreasing part after the peak). If both conditions are met, calculate the length of the mountain as `lis[i] + lds[i] - 1`. Update the `max_len` if the current mountain length is greater.
- Step 5: Return the minimum number of removals, which is `n - max_len`.
Key Insights
- Insight 1: The problem can be solved by finding the longest mountain subarray within the given array. This involves identifying a peak and calculating the lengths of the increasing sequence before the peak and the decreasing sequence after the peak.
- Insight 2: Dynamic Programming can be used to efficiently compute the lengths of the Longest Increasing Subsequence (LIS) ending at each index and the Longest Decreasing Subsequence (LDS) starting at each index.
- Insight 3: To form a valid mountain, both the increasing and decreasing parts must have lengths greater than 1. This is because the peak element must have elements both before and after it.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n)
Topics
This problem involves: Array, Binary Search, Dynamic Programming, Greedy.
Companies
Asked at: PayPal.