LeetCode 1671: Minimum Number of Removals to Make Mountain Array Solution
Master LeetCode problem 1671 (Minimum Number of Removals to Make Mountain Array), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1671. Minimum Number of Removals to Make Mountain Array
Problem Explanation
Explanation:
To find the minimum number of elements to remove to make the given array a mountain array, we can iterate through each index of the array and treat that index as the peak of the mountain. Then, we can count the number of elements that need to be removed on the left side of the peak to maintain the increasing order and on the right side of the peak to maintain the decreasing order. The sum of these removals will give us the total elements to remove for that peak. Finally, we return the minimum total removals across all possible peaks.
- Iterate through each index as a potential peak.
- For each peak, find the longest increasing subsequence to the left and the longest decreasing subsequence to the right.
- The total elements to remove for this peak is the length of the array minus the sum of the longest increasing subsequence on the left and the longest decreasing subsequence on the right.
- Update the minimum total removals across all peaks.
Time Complexity:
The time complexity of this approach is O(n^2) where n is the length of the input array.
Space Complexity:
The space complexity of this approach is O(n) for storing the length of increasing subsequences.
Solution Code
class Solution {
public int minimumMountainRemovals(int[] nums) {
int n = nums.length;
int[] lisLeft = new int[n];
int[] ldsRight = new int[n];
for (int i = 0; i < n; i++) {
lisLeft[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
lisLeft[i] = Math.max(lisLeft[i], lisLeft[j] + 1);
}
}
}
for (int i = n-1; i >= 0; i--) {
ldsRight[i] = 1;
for (int j = n-1; j > i; j--) {
if (nums[j] < nums[i]) {
ldsRight[i] = Math.max(ldsRight[i], ldsRight[j] + 1);
}
}
}
int minRemovals = n;
for (int i = 1; i < n-1; i++) {
if (lisLeft[i] > 1 && ldsRight[i] > 1) {
minRemovals = Math.min(minRemovals, n - (lisLeft[i] + ldsRight[i] - 1));
}
}
return minRemovals;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1671 (Minimum Number of Removals to Make Mountain Array)?
This page provides optimized solutions for LeetCode problem 1671 (Minimum Number of Removals to Make Mountain Array) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1671 (Minimum Number of Removals to Make Mountain Array)?
The time complexity for LeetCode 1671 (Minimum Number of Removals to Make Mountain Array) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1671 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1671 in Java, C++, or Python.