LIS
354. Russian Doll Envelopes
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)
Example: Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
class Solution {
public:
int maxEnvelopes(vector<pair<int, int>>& envelopes) {
sort(envelopes.begin(), envelopes.end(), [](pair<int, int>&a, pair<int, int>&b){
return a.first == b.first ? a.second > b.second : a.first < b.first;
});
vector<int> heights;
size_t i = 0;
while (i < envelopes.size()) {
int cur = envelopes[i].second;
heights.push_back(cur);
++i;
}
vector<int> res;
for (auto h : heights) {
auto it = lower_bound(res.begin(), res.end(), h);
if (it == res.end()) {
res.push_back(h);
} else {
*it = h;
}
}
return res.size();
}
};