题目描述:

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)

解题思路:

这道题个人觉得没有别的hard难度的题复杂。难点可能在于对数据的操作。为此我新建一个”信封“类,把二维数组的每一行,即每一个“信封”封装成了一个对象,存在数组中。还新建了一个MyCompatator类方便为信封数组进行排序。经过这样的抽象化步骤,这个问题就变成了很简单的有序数组中二次循环找最大值的问题。这就十分容易解决了。

具体代码:

 public class Solution {

      public static int maxEnvelopes(int[][] envelopes) {
//特殊情况的判断
if(envelopes==null||envelopes.length==0)
return 0;
if(envelopes.length==1)
return 1;
A[] array = new A[envelopes.length];
for(int i=0;i<envelopes.length;i++){
array[i] =new A(envelopes[i][0],envelopes[i][1]);
}
//为所有信封按“大小”排序
MyCompatator m = new MyCompatator();
Arrays.sort(array,m);
int[] result = new int[envelopes.length]; int max =1;
//很简单的循环操作找最大值,不在细讲
for(int i=0;i<envelopes.length;i++){
result[i]=1;
for(int j=0;j<i;j++){
if( (array[j].w<array[i].w) && (array[j].h<array[i].h) ){
result[i] = Math.max(result[i], 1+result[j]);
}
}
if(max<result[i]){
max=result[i];
}
}
return max;
}
}
//比较器,用来为数组排序
class MyCompatator implements Comparator<A>{ @Override
public int compare(A o1, A o2) {
if(o1.w>o2.w){
return 1;
}
else if(o1.w<o2.w){
return -1;
}
else{
if(o1.h>o2.h){
return 1;
}
else if(o1.h<o2.h){
return -1;
}
else
return 0;
}
} }
//信封类
class A{
int w;
int h;
public A(int w,int h){
this.w=w;
this.h=h;
}
}

【leetcode】354. Russian Doll Envelopes的更多相关文章

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

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

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

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

  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. 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】二分 binary_search(共58题)

    [4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...

  8. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  9. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

随机推荐

  1. “WinMount”和“云端”真是相当好用!

    WinMount作为一款压缩文件管理以及虚拟光驱工具已经无敌了.更有两项功能相当好用: 1.将rar.zip等压缩文件直接虚拟成磁盘,也就是下载一个7G的游戏可以不用解压直接安装了! 2.右键压缩文件 ...

  2. Metadata Lock原理1

    https://www.percona.com/blog/2013/02/01/implications-of-metadata-locking-changes-in-mysql-5-5/ impli ...

  3. VBA Excel 打印

    1. 设置 页边距.打印区域 With .PageSetup .HeaderMargin = Application.CentimetersToPoints(0.5) .LeftMargin = Ap ...

  4. Redis HyperLogLog

      Redis 在 2.8.9 版本添加了 HyperLogLog 结构. Redis HyperLogLog 是用来做基数统计的算法,HyperLogLog 的优点是,在输入元素的数量或者体积非常非 ...

  5. String StringBuilder以及StringBuffer

    例一:[看了威哥视频,下面更好理解] package sunjava; public class String_test { public static void main(String[] args ...

  6. 视频-某hadoop高级应用-搜索提示

    看了北风的免费视频,只有一个案例,苦逼买不起几百上千的视频教程 先搭建简单的web项目,基于struts,使用到了bootstrap. 界面: web.xml <filter> <f ...

  7. C - Minimum Inversion Number

    Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs ( ...

  8. The method load(Class, Serializable) in the type HibernateTemplate is not applicable for the arguments (Class, int)

    引入别人的项目发现利用HibernateTemplate的load的方法报错了.错误提示为: The method load(Class, Serializable) in the type Hibe ...

  9. (转载)MatLab绘图

    转载自:http://www.cnblogs.com/hxsyl/archive/2012/10/10/2718380.html 转载自:http://www.cnblogs.com/jeromebl ...

  10. vsftp安装与配置

    配置参考:https://help.aliyun.com/knowledge_detail/5973912.html?spm=5176.776701992.0.0.3X2PB8 553 Could n ...