-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1964.cpp
More file actions
21 lines (21 loc) · 1008 Bytes
/
1964.cpp
File metadata and controls
21 lines (21 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
vector<int> longestObstacleCourseAtEachPosition(vector<int>& obstacles) {
vector<int>lis;//longest increasing subsequence
for(int i=0;i<obstacles.size();i++){
int t=obstacles[i];
if(lis.empty() || lis[lis.size()-1]<=t){
lis.push_back(t);
obstacles[i]=lis.size();
}else{
int indx=upper_bound(lis.begin(),lis.end(),t)-lis.begin();
//The upper_bound() function returns an iterator pointing to the first element in the range [lis.begin(), lis.end()) that is greater than x. If no such element is found, it returns the iterator lis.end()
//Subtracting the iterator pointing to the beginning of lis from the resulting iterator gives the index of the upper bound
lis[indx]=t;
//Updating/forming longest increasing subsequence
obstacles[i]=indx+1;
}
}
return obstacles;
}
};