-
Notifications
You must be signed in to change notification settings - Fork 0
/
RussianDoll.txt
42 lines (33 loc) · 1.27 KB
/
RussianDoll.txt
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
354. Russian Doll Envelopes
You are given a 2D array of integers envelopes where envelopes[i] = [wi, hi] represents the width and the height of an envelope.
One envelope can fit into another if and only if both the width and height of one envelope are
greater than the other envelope's width and height.
Return the maximum number of envelopes you can Russian doll (i.e., put one inside the other).
Note: You cannot rotate an envelope.
class Solution {
public:
static bool compare(vector<int>& a, vector<int>& b){
if(a[0]==b[0])
return a[1]>b[1];
return a[0]<b[0];
}
int mostOptimal(vector<vector<int>>& envelopes){
vector<int> ans;
ans.push_back(envelopes[0][1]);
for(int i=1;i<envelopes.size();i++){
if(envelopes[i][1]>ans.back())
ans.push_back(envelopes[i][1]);
else{
int index=lower_bound(ans.begin(),ans.end(),envelopes[i][1])-ans.begin();
ans[index]=envelopes[i][1];
}
}
return ans.size();
}
int maxEnvelopes(vector<vector<int>>& envelopes) {
sort(envelopes.begin(),envelopes.end(),compare);
int prev=-1;
int curr=0;
return mostOptimal(envelopes);
}
};