[LeetCode] 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)
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 is3
([2,3] => [5,4] => [6,7]).
信封的嵌套问题,就像俄罗斯套娃一样,一个套一个,求能套的最多数量。
和300. Longest Increasing Subsequence类似,从那题一维变成了两维。
先给信封排序,按信封的宽度从小到大排,宽度相等时,高度大的在前面。问题就简化了成了找高度数字中的Longest Increasing Subsequence。
Java: Naive
public int maxEnvelopes(int[][] envelopes) {
if(envelopes==null||envelopes.length==0)
return 0; Arrays.sort(envelopes, new Comparator<int[]>(){
public int compare(int[] a, int[] b){
if(a[0]!=b[0]){
return a[0]-b[0];
}else{
return a[1]-b[1];
}
}
});
int max=1;
int[] arr = new int[envelopes.length];
for(int i=0; i<envelopes.length; i++){
arr[i]=1;
for(int j=i-1; j>=0; j--){
if(envelopes[i][0]>envelopes[j][0]&&envelopes[i][1]>envelopes[j][1]){
arr[i]=Math.max(arr[i], arr[j]+1);
}
}
max = Math.max(max, arr[i]);
} return max;
}
Java: Binary Search
public int maxEnvelopes(int[][] envelopes) {
if(envelopes==null||envelopes.length==0)
return 0; Arrays.sort(envelopes, new Comparator<int[]>(){
public int compare(int[] a, int[] b){
if(a[0]!=b[0]){
return a[0]-b[0]; //ascending order
}else{
return b[1]-a[1]; // descending order
}
}
}); ArrayList<Integer> list = new ArrayList<Integer>(); for(int i=0; i<envelopes.length; i++){ if(list.size()==0 || list.get(list.size()-1)<envelopes[i][1])
list.add(envelopes[i][1]); int l=0;
int r=list.size()-1; while(l<r){
int m=l+(r-l)/2;
if(list.get(m)<envelopes[i][1]){
l=m+1;
}else{
r=m;
}
} list.set(r, envelopes[i][1]);
} return list.size();
}
Python:
class Solution(object):
def maxEnvelopes(self, envelopes): def insert(target):
left, right = 0, len(result) - 1
while left <= right:
mid = left + (right - left) / 2
if result[mid] >= target:
right = mid - 1
else:
left = mid + 1
if left == len(result):
result.append(target)
else:
result[left] = target result = []
envelopes.sort(lambda x, y: y[1] - x[1] if x[0] == y[0] else \
x[0] - y[0])
for envelope in envelopes:
insert(envelope[1]) return len(result)
C++:
class Solution {
public:
int maxEnvelopes(vector<pair<int, int>>& envelopes) {
vector<int> dp;
sort(envelopes.begin(), envelopes.end(), [](const pair<int, int> &a, const pair<int, int> &b){
if (a.first == b.first) return a.second > b.second;
return a.first < b.first;
});
for (int i = 0; i < envelopes.size(); ++i) {
int left = 0, right = dp.size(), t= envelopes[i].second;
while (left < right) {
int mid = left + (right - left) / 2;
if (dp[mid] < t) left = mid + 1;
else right = mid;
}
if (right >= dp.size()) dp.push_back(t);
else dp[right] = t;
}
return dp.size();
}
};
类似题目:
[LeetCode] 300. Longest Increasing Subsequence 最长递增子序列
All LeetCode Questions List 题目汇总
[LeetCode] 354. Russian Doll Envelopes 俄罗斯套娃信封的更多相关文章
- leetCode 354. Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- leetcode@ [354] Russian Doll Envelopes (Dynamic Programming)
https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths and ...
- 第十二周 Leetcode 354. Russian Doll Envelopes(HARD) LIS问题
Leetcode354 暴力的方法是显而易见的 O(n^2)构造一个DAG找最长链即可. 也有办法优化到O(nlogn) 注意 信封的方向是不能转换的. 对第一维从小到大排序,第一维相同第二维从大到小 ...
- 【leetcode】354. Russian Doll Envelopes
题目描述: You have a number of envelopes with widths and heights given as a pair of integers (w, h). One ...
- 354 Russian Doll Envelopes 俄罗斯娃娃信封
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- 354. Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- [Swift]LeetCode354. 俄罗斯套娃信封问题 | Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- [LeetCode] Russian Doll Envelopes 俄罗斯娃娃信封
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- 动态规划——Russian Doll Envelopes
这个题大意很好理解,通过例子就能明白,很像俄罗斯套娃,大的娃娃套小的娃娃.这个题是大信封套小信封,每个信封都有长和宽,如果A信封的长和宽都要比B信封的要大,那么A信封可以套B信封,现在给定一组信封的大 ...
随机推荐
- django项目使用layui插件给网站设置一个日历挂件,很简单实用。
进入https://www.layui.com/首页下载layui文件 下载解压后把文件放在static静态文件中, html页面引入css和js <link rel="stylesh ...
- 将mysql从MyISAM更改为INNODB
今天更新django中的表字段,由于mysql从5.1升级到5.7.以前的外键关联必须从MYISAM改新为INNODB才可以继续. 过程有点刺激,但还好,只要想清楚了过程,提前作好备份,就没啥大问题. ...
- MySQL数据的优化方案
一.选取最使用的字段属性 mysql可以使用的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快,因此在创建表的时候,为了获得更好的性能,我们可以将表中的字段的宽度尽量设 ...
- 项目Alpha冲刺(团队)-第九天冲刺
格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队) 团队名称:为了交项目干杯 作业目标:描述第九天冲刺的项目进展.问题困难.心得体会 队员姓名与学号 队员学号 ...
- python开发应用-本地数据获取方法
文件的打开.读写和关闭 文件的打开: file_obj=open(filename,mode='r',buffering=-1,...) filename是强制参数 mode是可选参数,默认值是r b ...
- Flume知识扩展
1 常见正则表达式语法 元字符 描述 ^ 匹配输入字符串的开始位置.如果设置了RegExp对象的Multiline属性,^也匹配“\n”或“\r”之后的位置. $ 匹配输入字符串的结束位置.如果设置了 ...
- hibernate笔记
1.hibernate中的list()遍历方法和iterator()遍历方法之间的区别 1:返回的类型不一样,list()返回List, iterate()返回Iterator,2: 获取数据的方式不 ...
- 我永远讨厌gch文件
一个学期没写博客了. 今天写OOP作业见鬼了, 调了半天. 我写了一个match.h和一个match.cpp, 然后match.cpp里面#include"match.h", 然后 ...
- 利用GitHub+Node.js+Hexo搭建个人博客
本篇是自己在搭建Hexo博客平台时的一个过程记录.(2019.9.13实测有效) GitHub 账号注册 因为此文所搭建的个人博客是基于GitHub平台服务的,所以首先是注册GitHub,当然已有账号 ...
- 六.深浅copy
先问问大家,什么是拷贝?拷贝是音译的词,其实他是从copy这个英文单词音译过来的,那什么是copy? copy其实就是复制一份,也就是所谓的抄一份.深浅copy其实就是完全复制一份,和部分复制一份的意 ...