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的更多相关文章

  1. 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 ...

  2. leetcode@ [354] Russian Doll Envelopes (Dynamic Programming)

    https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths and ...

  3. 【leetcode】354. Russian Doll Envelopes

    题目描述: You have a number of envelopes with widths and heights given as a pair of integers (w, h). One ...

  4. 354 Russian Doll Envelopes 俄罗斯娃娃信封

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  5. [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 ...

  6. 第十二周 Leetcode 354. Russian Doll Envelopes(HARD) LIS问题

    Leetcode354 暴力的方法是显而易见的 O(n^2)构造一个DAG找最长链即可. 也有办法优化到O(nlogn) 注意 信封的方向是不能转换的. 对第一维从小到大排序,第一维相同第二维从大到小 ...

  7. [LeetCode] Russian Doll Envelopes 俄罗斯娃娃信封

    You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...

  8. 动态规划——Russian Doll Envelopes

    这个题大意很好理解,通过例子就能明白,很像俄罗斯套娃,大的娃娃套小的娃娃.这个题是大信封套小信封,每个信封都有长和宽,如果A信封的长和宽都要比B信封的要大,那么A信封可以套B信封,现在给定一组信封的大 ...

  9. [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 ...

随机推荐

  1. SHELL脚本的常规命令

    **shell脚本的执行方式: 方法一:首先赋予x权限,再输入相对路径或绝对路径,./testdot.sh或/root/shell/testdot.sh 方法二:sh testdot.sh(会新开一个 ...

  2. 面试前赶紧看了5道Python Web面试题,Python面试题No17

    目录 本面试题题库,由公号:非本科程序员 整理发布 第1题: Flask中的请求上下文和应用上下文是什么? 第2题:django中间件的使用? 第3题: django开发中数据做过什么优化? 第4题: ...

  3. static关键字 详解

    原文地址:http://blog.csdn.net/keyeagle/article/details/6708077 google了近三页的关于C语言中static的内容,发现可用的信息很少,要么长篇 ...

  4. 转载: CRichEditCtrl使用大全

    richedit 常见使用问题 一.常见问题 a.可以编译,不能执行的 在需要在相应的对话框中加上InitInstance(void)函数中添加 AfxInitRichEdit(); b.升级默认的R ...

  5. MOTCF 没时间解释了 条件竞争漏洞

    moctf 没时间解释了 条件竞争漏洞 题目链接 条件竞争: 在本题目中,上传文件的时候服务器无条件的接收任何类型的文件,但是你上传之后服务器会给你的文件内容修改为too slow. 比如你上传了一句 ...

  6. PHP FILTER_VALIDATE_IP 过滤器

    FILTER_VALIDATE_IP 过滤器把值作为 IP 进行验证,用于验证 IP 是否合法,支持 IPV6 验证 例子 <?php $ip = "192.168.0.1" ...

  7. c++ 吕凤翥 第六章 类和对象(二)

    c++ 吕凤翥 第六章 类和对象(二) 指针   引用  和数组 一:对象指针和对象引用 1.指向类的成员的指针 分为指向成员变量和指向成员函数两种指针 成员变量的格式:     类型说明符  类名: ...

  8. python实现删除空文件夹 附源代码

    前言:空文件夹虽然不占空间,但是有时候看着确实挺烦的(别误会,我不是强迫症!),所以写了一个用于删除当前目录下的空文件夹的小程序 环境:win7 64位:python2.7:IDE pycharm20 ...

  9. PAT——乙级1012

    1012 数字分类 (20 point(s)) 给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字: A​1​​ = 能被 5 整除的数字中所有偶数的和: A​2​​ = 将被 5 除后 ...

  10. Linux内核使用毫秒延时函数

    毫秒延时函数:mdelay() 微妙延时函数:ndelay() #ifndef mdelay #define mdelay(n) (/ (__builtin_constant_p(n) &&a ...