-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy path1671-minimum-number-of-removals-to-make-mountain-array.js
49 lines (44 loc) · 1.39 KB
/
1671-minimum-number-of-removals-to-make-mountain-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 1671. Minimum Number of Removals to Make Mountain Array
* https://leetcode.com/problems/minimum-number-of-removals-to-make-mountain-array/
* Difficulty: Hard
*
* 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.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var minimumMountainRemovals = function(nums) {
const length = nums.length;
const leftLIS = new Array(length).fill(1);
const rightLIS = new Array(length).fill(1);
for (let i = 1; i < length; i++) {
for (let j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
leftLIS[i] = Math.max(leftLIS[i], leftLIS[j] + 1);
}
}
}
for (let i = length - 2; i >= 0; i--) {
for (let j = length - 1; j > i; j--) {
if (nums[i] > nums[j]) {
rightLIS[i] = Math.max(rightLIS[i], rightLIS[j] + 1);
}
}
}
let maxMountainLength = 0;
for (let i = 1; i < length - 1; i++) {
if (leftLIS[i] > 1 && rightLIS[i] > 1) {
maxMountainLength = Math.max(maxMountainLength, leftLIS[i] + rightLIS[i] - 1);
}
}
return length - maxMountainLength;
};