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

    Example:
    Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

     1 public class Solution {
     2     public int maxEnvelopes(int[][] envelopes) {
     3         if(envelopes==null||envelopes.length==0||envelopes[0]==null||envelopes[0].length!=2) return 0;
     4         Arrays.sort(envelopes,new Comparator<int[]>(){
     5             public int compare(int[] i1,int[] i2){
     6                 if(i1[0]==i2[0]) return i2[1]-i1[1];
     7                 else return i1[0]-i2[0];
     8             }
     9         });
    10         int[] dp = new int[envelopes.length];
    11         int len = 0;
    12         for(int[] envelope:envelopes){
    13             int end =binarysearch(dp,0,len,envelope[1]);
    14             if(end==len) len++;
    15         }
    16         return len;
    17     }
    18     public int binarysearch(int[] dp,int left,int right,int target){
    19         while(left<right){
    20             int mid = left+(right-left)/2;
    21             if(dp[mid]>=target) right = mid;
    22             else left = mid+1;
    23         }
    24         dp[left] = target;
    25         return left;
    26     }
    27 }
    28 //the time complexity could be nlogn, the space complexity could be O(n);
  • 相关阅读:
    python 登陆接口
    FTP 的搭建过程和遇到的问题
    ELK 的好文章连接
    logstash 配置文件实例
    使用 screen 管理你的远程会话
    Conda常用命令整理
    python 增加矩阵行列和维数
    26个你不知道的Python技巧
    python单下划线、双下划线、头尾双下划线说明:
    python 类方法的互相调用及self的含义
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6939599.html
Copyright © 2011-2022 走看看