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);
  • 相关阅读:
    10 个迅速提升你 Git 水平的提示
    GitLab-CI与GitLab-Runner
    WEB应用安全解决方案测试验证
    sparse representation 与sparse coding 的区别的观点
    The Ph.D. Grind
    Potential Pythonic Pitfalls
    Overfitting & Regularization
    LVM的一般操作过程
    你跟大牛之间仅仅差一个google
    Hadoop伪分布式模式部署
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6939599.html
Copyright © 2011-2022 走看看