-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path354. Russian Doll Envelopes.cpp
50 lines (37 loc) · 1.16 KB
/
354. Russian Doll Envelopes.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
class Solution {
public:
int binarySearch(vector<int>& vec, int maxVal, vector<vector<int>>& envelopes, int i){
int l = 0;
int r = maxVal;
int temp = maxVal;
while(l <= r){
int mid = l + (r - l) / 2;
if(vec[mid] < envelopes[i][1]) l = mid + 1;
else {
temp = mid;
r = mid - 1;
}
}
return temp;
}
static bool compare(vector<int>& v1, vector<int>& v2) {
if(v1[0] == v2[0])
return v1[1] > v2[1];
return v1[0] < v2[0];
}
int maxEnvelopes(vector<vector<int>>& envelopes) {
int n = envelopes.size();
if(n == 0)
return 0;
sort(envelopes.begin(), envelopes.end(), compare);
vector<int> vec(n, 0);
int maxVal = 0;
for(int i = 0; i < n; i++) {
int pos = binarySearch(vec, maxVal, envelopes, i);
vec[pos] = envelopes[i][1];
if(pos == maxVal)
maxVal++;
}
return maxVal;
}
};