题目描述:

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. Android-L-Samples

    https://github.com/s3xy4ngyc/Android-L-Samples

  2. U盘安装Win7 64位系统(笔记本+台式机亲测)

    准备工具: 1. Win7 64位系统的镜像文件(网上随便一搜即可,最好是纯净版,没有一堆乱七八糟的内置软件) 2. 4G以上的U盘一个 所用软件: 老毛桃(官网下载) 具体步骤: 1.数据备份(将原 ...

  3. 用VS2010开发Android应用的配置方法

    在开发你的第一个Android应用程序之前,你应该先检查一下是否安装了Android SDK,以及是否创建好了Android模拟器(AVD),如果有不清楚的地方,请先看百度这篇文章“Android是什 ...

  4. JUnit---Java单元测试必备工具

      在我们每天业务代码都写不完,为什么还要写测试代码呢,项目完成之后我再测不行吗? 看起来像是增加了代码量,但是它恰恰帮你减少了后期测试排错的时间,每个team,每个人都是有Bug指标的,首先,每次你 ...

  5. CentOS中查看系统资源占用情况的命令

    用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O 负载情况 ...

  6. c语言字符串实例

    例子:涉及字符串.字符.指针.++等 例一:字符串与字符 #include <stdio.h> void reverse(char *str) { char *end=str; print ...

  7. eclipse 如何使用svn

    1.安装:安装包或输入网址 2.点打开透视图,点svn右键,新建网络地址 3.更新svn,检出svn即可

  8. 解决位图失真-SetStretchBltMode()

    当用以下函数加载一张位图时,当窗口发生重绘更改大小时,位图将失真: CBitmap bitmap;  bitmap.LoadBitmap(IDB_BITMAP2); BITMAP bmp;  bitm ...

  9. storyBoard中的Segue跳转

    //———————————————--------------在不确定的Segue跳转-----------------------------------   多个按钮指向要跳转的视图 1.在一个恰 ...

  10. 使用markdown及highlight

    一.markdown 安装markdown2 pip install markdown2 应用markdown2 进入blog APP,创建templatetags文件夹,在文件夹内创建__init_ ...