leetcode@ [354] Russian Doll envelopes (Dynamic programming)

Source: Internet
Author: User

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

You had 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 h Eight of the other envelope.

What is the maximum number of envelopes can 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 are 3 ([2,3] = [5,4] = [6,7]).

classPair { Public intwidth;  Public intheight;  PublicPairintWinth) {Super();  This. width =W;  This. Height =h; }}classPaircomparatorImplementsComparator { Public intCompare (Object O1, Object O2) {pair P1=(pair) O1; Pair P2=(pair) O2; if(P1.width <p2.width) {return-1; } Else if(P1.width = =p2.width) {if(P1.height = =p2.height) {return0; } Else if(P1.height <p2.height) {return-1; } Else {                return1; }                    } Else {                        return1; }    }} Public classSolution { Public intMaxenvelopes (int[] envelopes) {                intn =envelopes.length; if(n = = 0) {            return0; } pair pr[]=NewPair[n];  for(inti=0; i<n; ++i) {pair P=NewPair (Envelopes[i][0], envelopes[i][1]); Pr[i]=p; } arrays.sort (PR,Newpaircomparator ()); int[] DP =New int[n]; intrs = 1;  for(inti=0; i<n; ++i) {intMmax = 0;  for(intpre=0; pre<i; ++pre) {                if(Pr[pre].width < pr[i].width && Pr[pre].height <pr[i].height) {Mmax=Math.max (Mmax, Dp[pre]); }} Dp[i]= Mmax + 1; RS=Math.max (RS, dp[i]); }                returnrs; }}
View Code

[email protected] [354] Russian Doll envelopes (Dynamic programming)

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.