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

    DP 解法, Time Complexity: O(N^2)

    sort based on width or height, anyone is ok. After the sorting, for each envelope, those envelopes which can fit into this one can only be in the front.

    then maintain an array dp[i], where dp[i] is the max number of envelopes that can fit into envelope i.

     1 public class Solution {
     2     public int maxEnvelopes(int[][] envelopes) {
     3         if(envelopes == null || envelopes.length == 0 || envelopes[0] == null || envelopes[0].length != 2)
     4             return 0;
     5         int maxDoll = Integer.MIN_VALUE;
     6         int[] dp = new int[envelopes.length];
     7         Comparator<int[]> comp = new Comparator<int[]>(){
     8             public int compare(int[] arr1, int[] arr2) {
     9                 return arr1[0] - arr2[0];
    10             }
    11         };
    12         Arrays.sort(envelopes, comp);
    13         
    14         for (int i=0; i<dp.length; i++) {
    15             dp[i] = 1; //initialize
    16             for (int j=0; j<i; j++) {
    17                 if (envelopes[j][0]<envelopes[i][0] && envelopes[j][1]<envelopes[i][1]) {
    18                     dp[i] = Math.max(dp[i], dp[j]+1);
    19                 }
    20             }
    21             maxDoll = Math.max(maxDoll, dp[i]);
    22         }
    23         return maxDoll;
    24     }
    25 }

    Better Solution: Also DP, find longest increasing subsequence in the array.  Time Complexity: O(NlogN)

    1. Sort the array. Ascend on width and descend on height if width are same.
    2. Find the longest increasing subsequence based on height.

    Notice: 

    • Since the width is increasing, we only need to consider height.
    • [3, 4] cannot contains [3, 3], so we need to put [3, 4] before [3, 3] when sorting otherwise it will be counted as an increasing number if the order is [3, 3], [3, 4]

    Arrays.binarysearch(int[] a, int fromIndex, int toIndex, int key) if not find key int array a, will return -(insertionPosition)-1, The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key.  toIndex is the index of the last element (exclusive) to be searched. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

     1 public int maxEnvelopes(int[][] envelopes) {
     2     if(envelopes == null || envelopes.length == 0 
     3        || envelopes[0] == null || envelopes[0].length != 2)
     4         return 0;
     5     Arrays.sort(envelopes, new Comparator<int[]>(){
     6         public int compare(int[] arr1, int[] arr2){
     7             if(arr1[0] == arr2[0])
     8                 return arr2[1] - arr1[1];
     9             else
    10                 return arr1[0] - arr2[0];
    11        } 
    12     });
    13     int dp[] = new int[envelopes.length];
    14     int len = 0;
    15     for(int[] envelope : envelopes){
    16         int index = Arrays.binarySearch(dp, 0, len, envelope[1]);
    17         if(index < 0)
    18             index = -(index + 1);
    19         dp[index] = envelope[1];
    20         if(index == len)
    21             len++;
    22     }
    23     return len;
    24 }
  • 相关阅读:
    最佳的思维导图生成工具——markmap 使用教程
    07. struts2中对Action的管理方式
    06. struts2中指定struts2处理的请求后缀
    05. struts2中为Action属性注入值
    03. struts2中Action配置的各项默认值
    04. struts2中Result配置的各种视图转发类型
    02. struts2中Action名称的搜索顺序
    NSAttributedString使用CSS+html创建换行符无效( 无效)处理方法
    UITextView 添加到UITableViewCell上使用AttributedString点击链接不调用代理方法的处理方法及自定义link样式需要注意的问题
    iOS TableView reaload delete的实际操作
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6100233.html
Copyright © 2011-2022 走看看