354 Russian Doll Envelopes
1. Question
2. Implementation
class Solution {
public int maxEnvelopes(int[][] envelopes) {
if (envelopes == null || envelopes.length == 0) {
return 0;
}
Arrays.sort(envelopes, new Comparator<int[]>() {
@Override
public int compare(int[] a, int b[]) {
if (a[0] == b[0]) {
return b[1] - a[1];
}
else {
return a[0] - b[0];
}
}
});
int n = envelopes.length;
int[] dp = new int[n];
Arrays.fill(dp, 1);
int maxLen = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (envelopes[j][1] < envelopes[i][1] && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
}
}
maxLen = Math.max(maxLen, dp[i]);
}
return maxLen;
}
}3. Time & Space Complexity
Last updated