-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1671.cpp
More file actions
31 lines (29 loc) · 884 Bytes
/
1671.cpp
File metadata and controls
31 lines (29 loc) · 884 Bytes
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
class Solution {
public:
vector<int> LIS(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n, 1);
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[i] > nums[j]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
return dp;
}
int minimumMountainRemovals(vector<int>& nums) {
vector<int> left = LIS(nums);
reverse(nums.begin(), nums.end());
vector<int> right = LIS(nums);
reverse(right.begin(), right.end());
int n = nums.size();
int maxLength = 0;
for (int i = 1; i < n - 1; ++i) {
if (left[i] == 1) continue;
if (right[i] == 1) continue;
maxLength = max(maxLength, left[i] + right[i] - 1);
}
return n - maxLength;
}
};