-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Number_of_Removals_to_Make_Mountain_Array.cpp
More file actions
61 lines (51 loc) · 1.44 KB
/
Copy pathMinimum_Number_of_Removals_to_Make_Mountain_Array.cpp
File metadata and controls
61 lines (51 loc) · 1.44 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
//https://leetcode.com/problems/minimum-number-of-removals-to-make-mountain-array/
#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <string>
#define maxn 1001
using namespace std;
vector<int> nums={100,92,89,77,74,66,64,66,64};//{2,1,1,5,6,2,3,1} {4,3,2,1,1,2,3,1} {1,2,3,4,4,3,2,1};
long lleft[maxn];//from left
long rright[maxn];//from right
int main(){
long id=0;
int sz = nums.size();
vector<long> lis(maxn, -1);
vector<long>::iterator it;
lis[0] = nums[0];
lleft[0] = 0;
rright[sz-1] = 0;
for(int i=1;i<sz;i++){
if(nums[i]>lis[id]){
lis[++id] = nums[i];
lleft[i] = id;
}else{
it = lower_bound(lis.begin(), lis.begin()+id+1, nums[i]);
lleft[i] = it-lis.begin();
*it = nums[i];
}
}
lis.clear();
lis.resize(maxn);
lis[0] = nums[sz-1];
id=0;
for(int i=sz-2;i>=0;i--){
if(nums[i]>lis[id]){
lis[++id] = nums[i];
rright[i] = id;
}else{
it = lower_bound(lis.begin(), lis.begin()+id+1, nums[i]);
rright[i] = it-lis.begin();
*it = nums[i];
}
}
int max = 0;
for(int i=1;i<sz-1;i++){
if((lleft[i]!= 0 && rright[i] != 0) && (lleft[i]+rright[i]>max)) {
max = lleft[i]+rright[i];
}
}
cout<<sz - max-1<<endl;
return 0;
}