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. __setitem__,__getitem,__delitem__的作用

    class Foo: def __init__(self, name): self.name = name def __getitem__(self, item): print('obj[key]时, ...

  2. 内存管理小结(2)--伙伴系统API

    伙伴系统分配内存以2的整数幂次的页数为单位.提供的API主要分为分配类与释放类. 1.分配类 1.1unsigned long __get_free_pages(gfp_t gfp_mask, uns ...

  3. A1027 Colors in Mars (20)(20 分)

    A1027 Colors in Mars (20)(20 分) People in Mars represent the colors in their computers in a similar ...

  4. HDOJ 2120 Ice_cream's world I

    Ice_cream's world I ice_cream's world is a rich country, it has many fertile lands. Today, the queen ...

  5. [转]9个基于Java的搜索引擎框架

    9个基于Java的搜索引擎框架 在这个信息相当繁杂的互联网时代,我们已经学会了如何利用搜索引擎这个强大的利器来找寻目标信息,比如你会在Google上搜索情人节如何讨女朋友欢心,你也会在百度上寻找正规的 ...

  6. Leetcode 514.自由之路

    自由之路 视频游戏"辐射4"中,任务"通向自由"要求玩家到达名为"Freedom Trail Ring"的金属表盘,并使用表盘拼写特定关键词 ...

  7. android TranslateAnimation动画执行时的坐标获取。

    android 的Tween动画并不会改变控件的属性值,比如以下测试片段: 定义一个从屏幕右边进入,滚动到屏幕左边消失的一个TranslateAnimation动画: <?xml version ...

  8. var和function定义方法的区别

    在JS中有两种定义函数的方式,1是var aaa=function(){...}2是function aaa(){...}var 方式定义的函数,不能先调用函数,后声明,只能先声明函数,然后调用.fu ...

  9. linux系统mysql连接检查脚本

    为了便于检查ECS服务器内部搭建的mysql或者RDS的mysql数据库,编写了一个mysql测试脚本,对于不熟悉命令行操作的朋友出现问题时可以检测一下.       脚本下载地址: http://j ...

  10. 【bzoj2836】魔法树 树链剖分+线段树

    题目描述 输入 输出 样例输入 4 0 1 1 2 2 3 4 Add 1 3 1 Query 0 Query 1 Query 2 样例输出 3 3 2 题解 树剖+线段树模板题,不过为什么写的人这么 ...