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]).
public class Solution {
public int maxEnvelopes(int[][] envelopes) {
if(envelopes==null||envelopes.length==0||envelopes[0]==null||envelopes[0].length!=2) return 0;
Arrays.sort(envelopes,new Comparator<int[]>(){
public int compare(int[] i1,int[] i2){
if(i1[0]==i2[0]) return i2[1]-i1[1];
else return i1[0]-i2[0];
}
});
int[] dp = new int[envelopes.length];
int len = 0;
for(int[] envelope:envelopes){
int end =binarysearch(dp,0,len,envelope[1]);
if(end==len) len++;
}
return len;
}
public int binarysearch(int[] dp,int left,int right,int target){
while(left<right){
int mid = left+(right-left)/2;
if(dp[mid]>=target) right = mid;
else left = mid+1;
}
dp[left] = target;
return left;
}
}
//the time complexity could be nlogn, the space complexity could be O(n);
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
题目描述: 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 ...
- [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(HARD) LIS问题
Leetcode354 暴力的方法是显而易见的 O(n^2)构造一个DAG找最长链即可. 也有办法优化到O(nlogn) 注意 信封的方向是不能转换的. 对第一维从小到大排序,第一维相同第二维从大到小 ...
- [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信封,现在给定一组信封的大 ...
- [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 ...
随机推荐
- windows下CMD命令大全(仅供参考)
CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本)chcp 修改默认字符集chcp 936默认中文chcp 650011. appwiz.c ...
- uncompressing linux .................................................后没反应解决办法
编译kernel是的no machine record defined 错误,网上有一些解法,其实都是错误的,以讹传讹.不打算自己写,找到一篇还算靠谱的,转摘一下. 其根本原因是没有在 __proc_ ...
- Python9-前端基础知识-day47
web开发本质: 1.浏览器输入网址回车都发生了什么? 1.浏览器给服务端发送一个消息 2.服务端拿到消息 3.服务端返回消息 4.浏览器展示页面C/S架构----B/S架构客户端和服务端 消息的格式 ...
- [USACO]Bovine Genomics
Description 给定两个字符串集合A,B,均包含N个字符串,长度均为M,求一个最短的区间[l,r],使得不存在字符串\(a\in A,b\in B,\)且\(a[l,r]=b[l,r]\) , ...
- GSMM数据库设计小结
边写边结 1.新增,删除,修改在各自的DAL中进行,查,可以新建一个DAL,里面是需要的各个属性,跨表,不同表属性整合成一个对象(集合)返回,输出到用户界面.
- getsupportfragmentmanager 没有这个方法
让activity继承自fragmentactivity就行了.
- Eclipse 读取config目录下文件
最近在一个项目,在项目下新建了一个config配置文件夹,添加一个配置文件config.properties. 使用classpath:config.properties方式加载配置文件, 具体实现代 ...
- python3与django中@property详解
django提供了内置装饰器 @staticmethod\@classmethod\property 在OSQA中,@property的使用频率是非常高的.下面就是它的使用方法: @property ...
- Gpfixup
Updated: April 17, 2012 Applies To: Windows Server 2003, Windows Vista, Windows Server 2008, Windows ...
- 再写一篇tps限流
再写一篇tps限流 各种限流算法的称呼 网上有很多文章介绍限流算法,但是对于这些算法的称呼与描述也是有点难以理解.不管那么多了.我先按我理解的维度梳理一下. 主要维度是:是正向计数还是反向计数.是定点 ...