题目描述:

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. w3cmark前端精彩博文周报 10.27-11.2

    w3cmark 官方Q群 145423956 | 官方微博 @w3cmark 这周周报来迟了,公司真的好忙!!!欢迎关注我们的微博 @w3cmark w3cmark推出每周精选前端博文推荐,通过阅读别 ...

  2. C加密解密

    /********************************************************* * des.h * 用户使用des算法头文件 * **************** ...

  3. inline(内联元素)和block(块级元素) 的区别

    块级元素,默认是独自占据一行的.比如是<p>.<h1>.<h2>.<h3>.<h4>.<h5>.<h6>.<u ...

  4. 如何在有实体键的情况下全部显示ActionBar的Menu?

    大伙都知道, 在老版本手机, 以及部分的新手机上都还残留实体键, 有了这些实体键, 默认菜单是用实体菜单键呼出的, 尽管你把android:showAsAction="always" ...

  5. centos安装memcache与telnet

    ####################linux下安装memcache过程######################http://www.cnblogs.com/zgx/archive/2011/ ...

  6. Java_Utils框架

    Spring Utils https://github.com/tangyanbo/springmore

  7. graylog2 架构--转载

    原文地址:http://docs.graylog.org/en/latest/pages/architecture.html Architectural considerations There ar ...

  8. IIS 允许无后缀文件访问的配置

    最近一个项目 前端开发用了一大堆无后缀的html模板,问题就是发布到IIS以后访问 模板文件报404错误.无法下载. 百度 谷歌 搜一堆 都是MIME里添加 '.*' 实际上无效 正解是: MIME里 ...

  9. SMI接口,SMI帧结构,MDC/MDIO

    转载:http://blog.csdn.net/zyboy2000/article/details/7442464 SMI全称是串行管理接口(Serial Management Interface). ...

  10. 深入研究Block用weakSelf、strongSelf、@weakify、@strongify解决循环引用(上)

    深入研究Block捕获外部变量和__block实现原理 前言 在上篇中,仔细分析了一下Block的实现原理以及__block捕获外部变量的原理.然而实际使用Block过程中,还是会遇到一些问题,比如R ...