leetcode 354. Matryoshka envelope problems (related to the sort of two-dimensional)

Title Description

Given the width and height of some tags envelope, width and height in integer form (w, h) appears. When the width and height than the other envelope big envelope this time, you can put the envelope in another envelope, like Russian dolls same. 
Calculate how many envelopes up to be able to form a group "Russian doll" envelope (which can put an envelope into another envelope inside). 

Note: 
No rotation envelope. 

Example: 

Input: Envelopes = [[5,4-], [6,4], [6, 7 are], [2,3 ]] 
Output: 3  
Explanation: The maximum number of envelopes 3, a combination of: [2, 3] => [5,4] => [6,7 ]. 

Source: stay button (LeetCode) 
link: HTTPS: // leetcode-cn.com/problems/russian-doll-envelopes 
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Problem-solving ideas:

Width first  w in ascending order, if they are  w the same, according to the height of the  h descending order. After all the  h as an array, the array is calculated in the LIS (longest increasing sequence) in length is the answer.

Code:

Import java.util.Arrays;
 Import java.util.Comparator; 

class Solution { 
    
    / ** 
     * the width w of the first ascending sort, if they are the same w, the height h is sorted in descending order. 
     After all the h * as an array, the array is calculated in the LIS (longest increasing sequence) in length is the answer. 
     * @Param Envelopes 
     * @return 
     * / 
    public  int maxEnvelopes ( int [] [] Envelopes) {
         int n-= envelopes.length;
         // in width ascending order, if the same width, height press descending 
        Arrays.sort (envelopes, new new Comparator < int []> () {
             public  int Compare (int [] A, int [] B) {
                 return A [0] == B [0] B [. 1] - A [. 1]:? A [0] - B [0 ]; 
            } 
        }); 
        // for the height of the array to find the LIS 
        int [] height = new new  int [n-];
         for ( int I = 0; I ++; I <n- ) 
            height [I] = Envelopes [I] [. 1 ]; 

        return lengthOfLIS (height); 
    } 

    / * * 
     * dynamic programming + binary search longest increasing subsequence 
     * dynamic programming + dichotomy 
     * @param nums 
     * @return 
     * / 
    public int lengthOfLIS ( int [] the nums) {
         int [] DP = new new  int [nums.length]; // length value representative sub-sequence dp [i] is the i, the value of this element in the end of the sequence. 
        int max = 0; // length of the longest sub-sequence 
        for ( int NUM: the nums) {
             int I = 0, J = max;
             the while (I < J) {
                 int m = (I + J) / 2 ;
                 IF (DP [m] < NUM) 
                    I = m +. 1 ;
                 the else 
                    J = m;
            } 
            DP [I] = num; // dichotomy find a value greater than the first covering num 
            max = Math.max (max, + I. 1); // updates the maximum length 
        }
         return max; 
    } 
}

 

Guess you like

Origin www.cnblogs.com/haimishasha/p/11461527.html