-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem300.cpp
More file actions
33 lines (33 loc) · 990 Bytes
/
problem300.cpp
File metadata and controls
33 lines (33 loc) · 990 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
32
33
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
//dynamic programming
/*
vector<int> store(nums.size() , 1);
for(int i = 0; i < nums.size(); i++){
for(int j = 0; j < i; j++){
if(nums[i] > nums[j]){
store[i] = max(store[i], store[j] + 1);
}
}
}
return *max_element(store.begin(), store.end());
*/
//tail method
vector<int> store;
for(int num: nums){
if(store.empty()||num > store.back()){
store.push_back(num);
}else{
store[firstGreatEqual(store, num)] = num;
}
}
return store.size();
}
private:
int firstGreatEqual(vector<int>& t, int tr){
//find index of target element
//lower bound solves from binary search
return lower_bound(t.begin(), t.end(), tr) - t.begin();
}
};