【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 }
时间: 2024-11-07 20:52:21

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

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

[email&#160;protected] [354] Russian Doll Envelopes (Dynamic Programming)

https://leetcode.com/problems/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 tha

[动态规划] leetcode 354 Russian Doll Envelopes

problem:https://leetcode.com/problems/russian-doll-envelopes/ 最长连续子序列类型问题.先排序,dp[i]记录使用第i个套娃的最大数量. bool cmp(const vector<int>& x, const vector<int>& y) { return x[0] == y[0] ? x[1] < y[1] : x[0] < y[0]; } class Solution { public:

第十二周 Leetcode 354. Russian Doll Envelopes(HARD) LIS问题

Leetcode354 暴力的方法是显而易见的 O(n^2)构造一个DAG找最长链即可. 也有办法优化到O(nlogn) 注意 信封的方向是不能转换的. 对第一维从小到大排序,第一维相同第二维从大到小排序. 维护一个符合题意的队列,当队列中的第二维均比当前信封的第二维小时,必然可以增加到队尾. 如果不然,可以让当前信封作为"替补",它可以在恰当的时候代替恰好比它大的信封. 当替补们足够替换所有已有信封时,就可以增加新的信封了. 比较抽象,不过这个技巧很有趣. 看代码吧,很清晰. cla

354. Russian Doll Envelopes

从我花时间上看,我是纠结排序很久.注意一下Comparator的写法,以后会了就好~ 算法上讲,就是对envolope尺寸排序,然后对于排序后的每个信封,它可以装进的最多小信封数,是长宽都比它小的信封中装的进最多数目+1.和之前做的368. Largest Divisible Subset思路是一样的,甚至还要简单一点. 动规. 1 static class MySort implements Comparator<int[]> { 2 public int compare(int[] o1,

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】二分 binary_search(共58题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find First and Last Position of Element in Sorted Array [35]Search Insert Position [50]Pow(

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n