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