zoukankan      html  css  js  c++  java
  • 【leetcode】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)

    解题思路:

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

    具体代码:

     1 public class Solution {
     2     
     3      public static int maxEnvelopes(int[][] envelopes) {
     4          //特殊情况的判断
     5          if(envelopes==null||envelopes.length==0)
     6              return 0;
     7          if(envelopes.length==1)
     8              return 1;
     9          A[] array = new A[envelopes.length];
    10          for(int i=0;i<envelopes.length;i++){
    11              array[i] =new A(envelopes[i][0],envelopes[i][1]);
    12          }
    13          //为所有信封按“大小”排序
    14          MyCompatator m = new MyCompatator();
    15          Arrays.sort(array,m);
    16          int[] result = new int[envelopes.length];
    17         
    18          int max =1;
    19          //很简单的循环操作找最大值,不在细讲
    20          for(int i=0;i<envelopes.length;i++){
    21              result[i]=1;
    22              for(int j=0;j<i;j++){
    23                  if( (array[j].w<array[i].w) && (array[j].h<array[i].h) ){
    24                      result[i] = Math.max(result[i], 1+result[j]); 
    25                  }
    26              }
    27              if(max<result[i]){
    28                  max=result[i];
    29              }
    30          }
    31          return max;
    32      }
    33 }
    34 //比较器,用来为数组排序
    35 class MyCompatator implements Comparator<A>{
    36 
    37     @Override
    38     public int compare(A o1, A o2) {
    39         if(o1.w>o2.w){
    40             return 1;
    41         }
    42         else if(o1.w<o2.w){
    43             return -1;
    44         }
    45         else{
    46             if(o1.h>o2.h){
    47                 return 1;
    48             }
    49             else if(o1.h<o2.h){
    50                 return -1;
    51             }
    52             else
    53                 return 0;
    54         }
    55     }
    56     
    57 }
    58 //信封类
    59 class A{
    60     int w;
    61     int h;
    62     public A(int w,int h){
    63         this.w=w;
    64         this.h=h;
    65     }
    66 }
  • 相关阅读:
    如何在 Windows 7 上安装 TeX Live 2018
    CF 964C Alternating Sum
    WF 18 A 想法
    CLion 使用笔记
    数理统计学复习
    ssh无密码访问设置(ssh-keygen 的详解)
    mysql开启远程连接的方法
    mysql命令大全
    Linux下创建仓库的软件包createrepo
    Linux 格式化分区 报错Could not stat --- No such file or directory 和 partprobe 命令
  • 原文地址:https://www.cnblogs.com/godlei/p/5566532.html
Copyright © 2011-2022 走看看