您的位置:首页 > 编程语言

编程练习(第十三周)

2017-05-14 09:15 375 查看


354. Russian Doll Envelopes

DescriptionHintsSubmissionsSolutions

Total Accepted: 18530
Total Submissions: 57918
Difficulty: Hard
Contributor: LeetCode

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]).

Subscribe to see which companies asked this question.

题解:

int maxEnvelopes(vector<pair<int, int>>& envelopes) {
auto Cmp = [](const pair<int, int> &a, const pair<int, int> &b) {
if(a.first < b.first) return true;
if(a.first == b.first && a.second > b.second) return true;
return false;
};
auto Cmp2 = [](const pair<int, int> &a, const pair<int, int> &b) {
return a.second < b.second;
};
sort(envelopes.begin(), envelopes.end(), Cmp);

vector<pair<int, int>> res;
for(int i=0; i<envelopes.size(); i++) {
auto it = std::lower_bound(res.begin(), res.end(), envelopes[i], Cmp2);
if(it == res.end()) res.push_back(envelopes[i]);
else *it = envelopes[i];
}
return res.size();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: