publicclassRussianDollEnvelopes{ /** * You have 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 height of the other envelope. * * What is the maximum number of envelopes can you Russian doll? (put one inside other) * * Note: * Rotation is not allowed. * * Example: * * Input: [[5,4],[6,4],[6,7],[2,3]] * Output: 3 * Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]) */ publicintmaxEnvelopes(int[][] envelopes){ if (envelopes.length == 0 || envelopes == null) return0; int n = envelopes.length; int[] dp = newint[envelopes.length]; sort(envelopes); int max_count=0; int max_idx = 0; for (int i=1; i<envelopes.length; i++){ for (int j=0; j<i; j++){ if (envelopes[i][0]>envelopes[j][0] && envelopes[i][1]>envelopes[j][1]){ dp[i] = Math.max(dp[i], dp[j] + 1); if (dp[i] > max_count){ max_count = dp[i]; max_idx = i; } } } } return dp[max_idx]+1;