Leetcode-354. Russian Doll Envelopes__leetcode

Source: Internet
Author: User

Topic:

You are have a number of envelopes with widths and heights given as a pair of integers (W, h). One envelope can fit to another if and only if both the width and height of one envelope is greater than the width and h Eight of the other envelope.

What is the maximum number of envelopes can you Russian doll? (Put one inside the 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]).

The problem is a Russian set of dolls, given a series of rectangles, ask how many can be nested together. The first reaction to the problem should be to sort all the elements in the matrix by width, then compare each one, and use an array to record the maximum number of rectangles the current rectangle can nest. If the next one is bigger than this, it's + 1; The code looks like this:

    public int maxenvelopes (int[][] envelopes) {
        //The matrix is sorted according to the first element
        Arrays.sort (envelopes, (A, B)-> a[0]-b[0]); C3/>int max = 0;
        Record the number of each rectangle that can be nested
        int DP [] = new int [envelopes.length];
        Iterate through all the rectangles for
        (int i = 0; i < envelopes.length i++) {
            Dp[i] = 1;//is at least 1, he
            //traverses all the rectangles in front of them, and if they can be nested inside themselves, their values are +1< c10/>for (int j = 0; J < i; J +) {
                if (Envelopes[j][0] < envelopes[i][0] && Envelopes[j][1] < envelopes I [1])
                    Dp[i] = Math.max (Dp[i], dp[j] + 1);
            }
            Save the maximum record so far;
            max = Math.max (Dp[i], max);
        return max;
    }

This short code has a higher time complexity because he has to traverse all the rectangles for each rectangle, only to beat 15% of the users. An improved method is to arrange the matrices in order of width, while the widths are sorted in descending degree by height. So we do not need to consider the width of the problem, only to consider the height of the array, according to the previous longest increasing subsequence this topic to find the height list of the largest increase ordinal group length can be. So after sorting we just need to look for the maximum sequence of an array, the code efficiency is greatly improved, can beat 90% of the users. Code in:

 public static int maxEnvelopes1 (int[][] envelopes) {if (envelopes = null | | envelope s.length = 0 | | ENVELOPES[0] = = NULL | |
        Envelopes[0].length!= 2) return 0;
                Arrays.sort (envelopes, New comparator<int[]> () {public int compare (int[] arr1, int[] arr2) {
                if (arr1[0] = = arr2[0]) return arr2[1]-arr1[1];
            else return arr1[0]-arr2[0];
        }
        });
        int dp[] = new Int[envelopes.length];
        int len = 0;
            For (int[] envelope:envelopes) {int index = Arrays.binarysearch (DP, 0, Len, envelope[1]);
            if (Index < 0) index =-(index + 1);
            Dp[index] = envelope[1];
        if (index = = len) len++;
    return Len; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.