博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: Russian Doll Envelopes
阅读量:6983 次
发布时间:2019-06-27

本文共 3114 字,大约阅读时间需要 10 分钟。

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]).

DP 解法, Time Complexity: O(N^2)

sort based on width or height, anyone is ok. After the sorting, for each envelope, those envelopes which can fit into this one can only be in the front.

then maintain an array dp[i], where dp[i] is the max number of envelopes that can fit into envelope i.

1 public class Solution { 2     public int maxEnvelopes(int[][] envelopes) { 3         if(envelopes == null || envelopes.length == 0 || envelopes[0] == null || envelopes[0].length != 2) 4             return 0; 5         int maxDoll = Integer.MIN_VALUE; 6         int[] dp = new int[envelopes.length]; 7         Comparator
comp = new Comparator
(){ 8 public int compare(int[] arr1, int[] arr2) { 9 return arr1[0] - arr2[0];10 }11 };12 Arrays.sort(envelopes, comp);13 14 for (int i=0; i

 

Better Solution: Also DP, find longest increasing subsequence in the array.  Time Complexity: O(NlogN)

  1. Sort the array. Ascend on width and descend on height if width are same.
  2. Find the  based on height.

 

Notice: 

  • Since the width is increasing, we only need to consider height.
  • [3, 4] cannot contains [3, 3], so we need to put [3, 4] before [3, 3] when sorting otherwise it will be counted as an increasing number if the order is [3, 3], [3, 4]

Arrays.binarysearch(int[] a, int fromIndex, int toIndex, int key) if not find key int array a, will return -(insertionPosition)-1, The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key.  toIndex is the index of the last element (exclusive) to be searched. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

 

1 public int maxEnvelopes(int[][] envelopes) { 2     if(envelopes == null || envelopes.length == 0  3        || envelopes[0] == null || envelopes[0].length != 2) 4         return 0; 5     Arrays.sort(envelopes, new Comparator
(){ 6 public int compare(int[] arr1, int[] arr2){ 7 if(arr1[0] == arr2[0]) 8 return arr2[1] - arr1[1]; 9 else10 return arr1[0] - arr2[0];11 } 12 });13 int dp[] = new int[envelopes.length];14 int len = 0;15 for(int[] envelope : envelopes){16 int index = Arrays.binarySearch(dp, 0, len, envelope[1]);17 if(index < 0)18 index = -(index + 1);19 dp[index] = envelope[1];20 if(index == len)21 len++;22 }23 return len;24 }

 

转载地址:http://ydtpl.baihongyu.com/

你可能感兴趣的文章
Sketch 快捷键
查看>>
javascript闭包,你大爷永远是你大爷
查看>>
android社会化分享
查看>>
PHPer面试指南-前言
查看>>
git 常用命令行
查看>>
前端面试——初(H)入(T)江(M)湖(L)
查看>>
支付宝小程序面向个人开放了!我将以一个 Demo 为例讲解整个流程。
查看>>
javascript 总结笔记
查看>>
WinForm连接数据库
查看>>
大快网站:如何选择正确的hadoop版本
查看>>
hadoop需要哪些技术支持
查看>>
赵童鞋带你入门PHP(六) ThinkPHP框架入门
查看>>
Java中断机制
查看>>
JS笔记(20): JS中的同步编程和异步编程
查看>>
Vue +Element Ui 使用Upload组件实现多图片上传
查看>>
那几个题(没懂的地方留言)
查看>>
如何改变UITableViewCell的选中样式(颜色)?storyboard上cell的selection不可用?
查看>>
Ubuntu 怎么增加根目录 大小
查看>>
Spring Cloud微服务分布式云架构—集成项目简介
查看>>
SQLServer之删除存储过程
查看>>