-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmain2.cpp
More file actions
35 lines (35 loc) · 1.14 KB
/
Copy pathmain2.cpp
File metadata and controls
35 lines (35 loc) · 1.14 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
class Solution {
public:
int minimumMountainRemovals(vector<int>& v) {
int n = v.size(),i,x;
vector<int> l(n,0),r(n,0),s;
s.push_back(v[0]);
for(i = 1; i < n; i++){//longest incresing subsequence from left
x = lower_bound(s.begin(),s.end(),v[i])-s.begin();
if(x==s.size()){
s.push_back(v[i]);
}else{
s[x] = v[i];
}
l[i] = i+1-s.size();
}
s.clear();
s.push_back(v[n-1]);
for(i = n-2; i >= 0; i--){//longest incresing subsequence from right
x = lower_bound(s.begin(),s.end(),v[i])-s.begin();
if(x==s.size()){
s.push_back(v[i]);
}else{
s[x] = v[i];
}
r[i] = n-1-i+1-s.size();
}
int ans = n;
for(i = 0; i < n; i++){
if(i+1-l[i]>=2 && n-i-r[i]>=2)//>=2 bcz from left and right at least 2 element must be there a<b>c
//i+1-l[i] and n-i-r[i] tells us no of element which we want delete from left and right.
ans = min(ans,l[i]+r[i]);
}
return ans;
}
};