[leetcode] 354. Russian Doll Envelopes
来源:互联网 发布:梦想云进销存软件 编辑:程序博客网 时间:2024/05/17 02:40
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.
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]).
我的代码
俄国沙皇问题
1.排序策略:假定二元组记为(a,b),先对a升序排列,当a相同时对b降序排列;
2.排序后只用看b, b的最长不降子序列就是最终的结果;
3.维持一个有效区ends,ends[i]记录b中长度为i+1的最长不降子序列的最小末尾;
注意:在本题中,当拿出的b中的值与ends中的某个值相等时,应该将其放入ends中与其相等的值的位置处,故这个时候应该更新r而不是l。
(更多见算法学习中的笔记)
class Solution {public: #define max(a,b) a>b?a:b static bool cmp(pair<int, int> env1, pair<int, int> env2) { if (env1.first!= env2.first) return env1.first<env2.first; else return env1.second > env2.second; } int maxEnvelopes(vector<pair<int, int> >& envelopes) { if (envelopes.empty()) { return 0; } else { sort(envelopes.begin(), envelopes.end(), cmp); int len=envelopes.size(); vector<int> ends; ends.resize(len); ends[0]=envelopes[0].second; int l=0, r=0; int Right=0; for (int i=1; i<len; i++) { l=0; r=Right; while(l<=r) { int ind=(l+r)/2; if (envelopes[i].second > ends[ind]) { l=ind+1; } else { r=ind-1; } } Right=max(Right,l); ends[l]=envelopes[i].second; } return Right+1; } }};
0 0
- LeetCode 354. Russian Doll Envelopes
- leetcode 354. Russian Doll Envelopes
- [leetcode] 354. Russian Doll Envelopes
- leetcode.354. Russian Doll Envelopes
- leetcode 354. Russian Doll Envelopes
- leetcode 354.Russian Doll Envelopes
- LeetCode 354. Russian Doll Envelopes
- Leetcode-354. Russian Doll Envelopes
- [LeetCode]354. Russian Doll Envelopes
- [leetcode] 354. Russian Doll Envelopes
- Leetcode 354. Russian Doll Envelopes
- [leetcode]354. Russian Doll Envelopes
- LeetCode 354. Russian Doll Envelopes
- 【Leetcode】Russian Doll Envelopes
- leetcode Russian Doll Envelopes
- LeetCode Russian Doll Envelopes
- LeetCode -- Russian Doll Envelopes
- 【Leetcode】Russian Doll Envelopes
- android viewPager 轮播图
- 判断文件格式
- SSH框架中的getHibernateTemplate.find()和session.createQuery()方法总结
- Premier Bob的算法模板库(II)
- oracle 基础---数据类型
- [leetcode] 354. Russian Doll Envelopes
- 商家如何使用微信预约
- 缓冲区溢出
- SylixOS中EEPROM设备驱动实现
- CodeIgniter表单验证
- Mac安装Brew
- java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingExc异常
- 学生信息管理系统
- Error: "ssdk_instapager_login_html" is not translated in "en" (English) [MissingTranslation]