354. Matryoshka envelope problem
Given the width and height of some tags envelope, width and height in integer form (w, h) appears. When the width and height than the other envelope big envelope this time, you can put the envelope in another envelope, like Russian dolls same.
Calculate how many envelopes up to be able to form a group "Russian doll" envelope (which can put an envelope into another envelope inside).
Note:
No rotation envelope.
Example:
Input: envelopes = [[5,4], [6,4], [6,7], [2,3]]
Output: 3
Explanation: The maximum number of envelopes 3, a combination of: [2,3] => [5,4] => [6,7].
class Solution {
public int maxEnvelopes(int[][] envelopes) {
int maxL = 0;
int[] dp = new int[envelopes.length];
Arrays.sort(envelopes, (a, b) -> (a[0] == b[0] ? b[1]-a[1] : a[0]-b[0]));
for(int[] env : envelopes) {
int lo = 0, hi = maxL;
while(lo < hi) {
int mid = lo+(hi-lo)/2;
if(dp[mid] < env[1])
lo = mid+1;
else
hi = mid;
}
dp[lo] = env[1];
if(lo == maxL)
maxL++;
}
return maxL;
}
}