Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c6f83d8

Browse files
committed
Add solution #1671
1 parent 9ca2edd commit c6f83d8

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,463 LeetCode solutions in JavaScript
1+
# 1,464 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1287,6 +1287,7 @@
12871287
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12881288
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
12891289
1670|[Design Front Middle Back Queue](./solutions/1670-design-front-middle-back-queue.js)|Medium|
1290+
1671|[Minimum Number of Removals to Make Mountain Array](./solutions/1671-minimum-number-of-removals-to-make-mountain-array.js)|Hard|
12901291
1672|[Richest Customer Wealth](./solutions/1672-richest-customer-wealth.js)|Easy|
12911292
1679|[Max Number of K-Sum Pairs](./solutions/1679-max-number-of-k-sum-pairs.js)|Medium|
12921293
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 1671. Minimum Number of Removals to Make Mountain Array
3+
* https://leetcode.com/problems/minimum-number-of-removals-to-make-mountain-array/
4+
* Difficulty: Hard
5+
*
6+
* You may recall that an array arr is a mountain array if and only if:
7+
* - arr.length >= 3
8+
* - There exists some index i (0-indexed) with 0 < i < arr.length - 1 such that:
9+
* - arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
10+
* - arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
11+
*
12+
* Given an integer array nums, return the minimum number of elements to remove to make
13+
* nums a mountain array.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @return {number}
19+
*/
20+
var minimumMountainRemovals = function(nums) {
21+
const length = nums.length;
22+
const leftLIS = new Array(length).fill(1);
23+
const rightLIS = new Array(length).fill(1);
24+
25+
for (let i = 1; i < length; i++) {
26+
for (let j = 0; j < i; j++) {
27+
if (nums[i] > nums[j]) {
28+
leftLIS[i] = Math.max(leftLIS[i], leftLIS[j] + 1);
29+
}
30+
}
31+
}
32+
33+
for (let i = length - 2; i >= 0; i--) {
34+
for (let j = length - 1; j > i; j--) {
35+
if (nums[i] > nums[j]) {
36+
rightLIS[i] = Math.max(rightLIS[i], rightLIS[j] + 1);
37+
}
38+
}
39+
}
40+
41+
let maxMountainLength = 0;
42+
for (let i = 1; i < length - 1; i++) {
43+
if (leftLIS[i] > 1 && rightLIS[i] > 1) {
44+
maxMountainLength = Math.max(maxMountainLength, leftLIS[i] + rightLIS[i] - 1);
45+
}
46+
}
47+
48+
return length - maxMountainLength;
49+
};

0 commit comments

Comments
 (0)