编程练习(第十三周)
2017-05-14 09:15
375 查看
354. Russian Doll Envelopes
DescriptionHintsSubmissionsSolutionsTotal 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(); }
相关文章推荐
- 机器学习 - 编程练习(一):线性回归
- Socket Tcp编程练习
- 程序设计入门——C语言 翁恺 第4周编程练习
- 【编程练习】八大排序算法
- C语言编程小练习
- C++ PRIMER PLUS (第六版) 中文版 第七章编程练习答案
- C Primer Plus 第二章 编程练习 1-7题
- C Primer Plus 第十二章 编程练习 1-8题
- 《C和指针》章节后编程练习解答参考——6.3
- C和指针之字符串编程练习6
- javascript之DOM编程根据属性找标签练习
- C++ Primer Plus 第六版 学习笔记 第七章 编程练习答案
- GEEK编程练习— —三数求和
- 【编程练习】kmp算法代码
- 《C和指针》章节后编程练习解答参考——6.6
- 无聊的练习编程
- C Primer Plus 16章 编程练习第7题
- C语言编程(练习1:字符串和字符串函数)
- 【[Offer收割]编程练习赛10 B】出勤记录II
- 模板元编程 第二章练习(非答案)