-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path354.cpp
53 lines (50 loc) · 1.24 KB
/
354.cpp
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
bool cmp(vector<int>& a,vector<int>& b){
if(a[0]<b[0]){
return 1;
}
else{
if(a[0]==b[0] && a[1]>b[1])
return 1;
return 0;
}
}
int lengthOfLIS(vector<int>& nums) {
int len = 1, n = (int)nums.size();
if (n == 0) {
return 0;
}
vector<int> d(n + 1, 0);
d[len] = nums[0];
for (int i = 1; i < n; ++i) {
if (nums[i] > d[len]) {
d[++len] = nums[i];
} else {
int l = 1, r = len, pos = 0,mid = 0;
while (l <= r) {
mid = (l + r) >> 1;
if (d[mid] < nums[i]) {
pos = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
d[pos + 1] = nums[i];
}
}
return len;
}
class Solution {
public:
int maxEnvelopes(vector<vector<int>>& envelopes) {
sort(envelopes.begin(),envelopes.end(),cmp);
vector<int> A(envelopes.size());
int i=0;
int j=0;
for(auto a:envelopes){
A[i]=a[1];
i++;
}
return lengthOfLIS(A);
}
};