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 ...
 
随机推荐
- JZOJ 5461. 【NOIP2017提高A组冲刺11.8】购物
			
5461. [NOIP2017提高A组冲刺11.8]购物 (File IO): input:shopping.in output:shopping.out Time Limits: 1000 ms ...
 - 06.VUE学习之非常实用的计算属性computed实例
			
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
 - 算法图解之大O表示法
			
什么是大O表示法 大O表示法可以告诉我们算法的快慢. 大O比较的是操作数,它指出了算法运行时间的增速. O(n) 括号里的是操作数. 举例 画一个16个格子的网格,下面分别列举几种不同的画法,并用大O ...
 - DFS:HDU1518-Square(剪枝较多的DFS)
			
题目: Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
 - Storm: 性能优化 (转载)
			
Storm 性能优化 原文地址:http://www.jianshu.com/p/f645eb7944b0 目录 场景假设 调优步骤和方法 Storm 的部分特性 Storm 并行度 Storm 消 ...
 - Diycode开源项目 TopicContentActivity分析
			
1.效果预览以及布局分析 1.1.实际效果预览 左侧话题列表的布局是通过TopicProvider来实现的,所以当初分析话题列表就没有看到布局. 这里的话题内容不是一个ListView,故要自己布局. ...
 - Spring---基于Spring IOC的小程序
			
实现的功能以及各文件间的关系 IHelloMessage:一个接口,用于定义输出问候信息. HelloWorld.HelloChina:接口的实现类.在这里表示人在不同的地方 Person:一个人物类 ...
 - 【读书笔记--cookie】JavaScript权威指南 第六版
			
遇到一些问题需要用cookie处理,正好读了一下犀牛书关于cookie的介绍,整理了一些笔记. cookie是指web浏览器存储的少量数据,同时它是与具体的web页面或者站点相关的. cookie数据 ...
 - 线段树&树状数组模板
			
树状数组: #include <bits/stdc++.h> using namespace std; ; struct binit { int a[MAXN], n; void modi ...
 - jquery实现轮播插件
			
这几天用jquery写了两个轮播的插件,功能很简单.第一次尝试写插件,有很多不足的地方,代码如下: 注:图片链接请替换掉,配置信息必须加上图片width和height. <!DOCTYPE ht ...