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)