-
Notifications
You must be signed in to change notification settings - Fork 0
/
LIS-354. Russian Doll Envelopes.cpp
45 lines (30 loc) · 1.14 KB
/
LIS-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
class Solution {
public:
/* complexity O(nlog(n))
space complexity O(n) since we are using extra vector space
Appproach : The idea is to sort envelopes width in increasing order if the two
envelopes has same width then sort then envelopes by its height in
decreasing order after that just do standard LIS of O(Nlog(N)) type
*/
static bool cmp(const vector<int>&v1,const vector<int>&v2){
if(v1[0]==v2[0]) {
return v1[1]>v2[1];
}
return v1[0]<v2[0];
}
int maxEnvelopes(vector<vector<int>>& envelopes) {
sort(envelopes.begin(),envelopes.end(),cmp);
vector<int>dp;
dp.push_back(envelopes[0][1]);
int n=envelopes.size();
for(int i=1;i<n;i++){
if(dp.back()<envelopes[i][1]){
dp.push_back(envelopes[i][1]);
}else{
auto it=lower_bound(dp.begin(),dp.end(),envelopes[i][1]);
*it=envelopes[i][1];
}
}
return dp.size();
}
};