You have a number of envelopes with widths and heights given as a pair of integers (w, h)
. One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.
What is the maximum number of envelopes can you Russian doll? (put one inside other)
Note:
Rotation is not allowed.
Example:
Input: [[5,4],[6,4],[6,7],[2,3]]
Output: 3
Explanation: The maximum number of envelopes you can Russian doll is 3
([2,3] => [5,4] => [6,7]).
/*
* 假设信封按w 排序 如果不存在相同h 找出递增h即可
* 但w重复时 [3,3] [3,4] 即是h构成递增序列 信封也是不能装入的
* 所以当w相同时h按降序排列 即 [3,4] [3,3] 此时4,3不构成递增序列
*
* */
class Solution {
public:
int maxEnvelopes(vector<vector<int>>& envelopes) {
auto f = [](const vector<int> &A, const vector<int> &B){
if(A[0]!=B[0]) return A[0]<B[0];
else return A[1]>B[1];
};
sort(envelopes.begin(), envelopes.end(), f);
// 存放一个"可能"的
vector<int> v;
for(auto &e:envelopes){
auto it = lower_bound(v.begin(), v.end(), e[1]);
if(it!=v.end()) *it = e[1];
else v.push_back(e[1]);
}
return v.size();
}
};