Leetcode 354. Russian Doll Envelopes

Source: Internet
Author: User

Title Link: https://leetcode.com/problems/russian-doll-envelopes/

Each doll has two dimensions, long and wide, and only two are larger than the corresponding dimension of the other doll to be able to fit another one.

This problem is actually lis.

Naïve practice directly after the O (n^2) to go DP, then since the problem can be regulated to the LIS, it is inevitable that O (nlogn) processing.

When sorting, a dimension such as W, according to small to large row, this is the first order, another dimension h according to from the big to the small row, this is the second order.

The reason is that the same doll, can not be nested, so h in the same case in W, according to from the big to the small row, you can guarantee that the same doll will not appear in the same situation.

Then the answer to the question is actually the LIS size of all the H-values that make up the sequence.

In Java, I didn't find an API similar to C + + that could do lower_bound on an array, so I could only write a lower_bound.

1  Public classSolution {2      Public intMaxenvelopes (int[] envelopes) {3Arrays.sort (envelopes, (x, y) (x[0] = y[0]? y[1]-x[1]: x[0]-y[0]));4         intn =envelopes.length;5         int[] DP =New int[n];6 Arrays.fill (DP, integer.max_value);7          for(inti = 0; I < n; i++) {8             intindex = Lower_bound (DP, envelopes[i][1]);9Dp[index] = envelopes[i][1];Ten         } One         returnLower_bound (DP, integer.max_value); A     } -  -     Private intLower_bound (int[] DP,inttarget) { the         intn =dp.length; -         intL = 0, r =N; -          while(L <r) { -             intm = L + R >>> 1; +             if(Dp[m] <target) { -L = m + 1; +}Else { AR =m; at             } -         } -         returnl; -     } -}
View Code

--------------------------------------------------------------------------------------------------------------- --------------

In other words, I think the leetcode of writing is like seeing more people than CF, TC, or some other OJ who don't know how many orders of magnitude ah.

Leetcode 354. Russian Doll Envelopes

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.