Skip to content

Commit 1da96fa

Browse files
authored
Update 1671-minimum-number-of-removals-to-make-mountain-array.js
1 parent b4ed200 commit 1da96fa

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

1671-minimum-number-of-removals-to-make-mountain-array.js

+38
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const minimumMountainRemovals = function(nums) {
6+
const inc = LIS(nums)
7+
const dec = LIS(nums.slice().reverse()).reverse()
8+
let res = 0
9+
for(let i = 0, len = nums.length; i < len; i++) {
10+
if(inc[i] > 1 && dec[i] > 1) res = Math.max(res, inc[i] + dec[i] - 1)
11+
}
12+
return nums.length - res
13+
};
14+
15+
function LIS(arr) {
16+
const stack = []
17+
const res = []
18+
for(let e of arr) {
19+
if((stack.length && e > stack[stack.length - 1]) || stack.length === 0) {
20+
stack.push(e)
21+
res.push(stack.length)
22+
continue
23+
}
24+
let l = 0, r = stack.length - 1
25+
while(l < r) {
26+
const mid = l + ((r - l) >> 1)
27+
if(stack[mid] < e) l = mid + 1
28+
else r = mid
29+
}
30+
stack[l] = e
31+
res.push(stack.length)
32+
}
33+
34+
return res
35+
}
36+
37+
// another
38+
139
/**
240
* @param {number[]} nums
341
* @return {number}

0 commit comments

Comments
 (0)