public class Solution {
private int findLess(int[] dp, int len, int target) {
int l = 0, r = len - 1;
while (l < r) {
int mid = l + (r - l) / 2;
int midNum = dp[mid];
if (midNum == target) return mid;
else if (midNum > target) {
r = mid;
} else {
l = mid + 1;
}
}
return l;
}
public int maxEnvelopes(int[][] envelopes) {
int len = envelopes.length;
Arrays.sort(envelopes, new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
if (a[0] == b[0]) {
//return a[1] - b[1]; [[4,5],[4,6],[6,7],[2,3],[1,1]]
return b[1] - a[1];
}
return a[0] - b[0];
}
});
int[] dp = new int[len];
int idx = 0;
for (int[] e : envelopes) {
if (idx == 0 || e[1] > dp[idx - 1]) {
dp[idx++] = e[1];
} else if (e[1] < dp[idx - 1]) {
int pos = findLess(dp, idx, e[1]);
dp[pos] = e[1];
}
}
return idx;
}
}
public class Solution {
public int maxEnvelopes(int[][] envelopes) {
if ( envelopes == null
|| envelopes.length == 0
|| envelopes[0] == null
|| envelopes[0].length == 0){
return 0;
}
Arrays.sort(envelopes, new Comparator<int[]>(){
@Override
public int compare(int[] e1, int[] e2){
return e1[0] - e2[0];
}
});
int n = envelopes.length;
int[] dp = new int[n];
int ret = 0;
for (int i = 0; i < n; i++){
dp[i] = 1;
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], 1 + dp[j]);
}
}
ret = Math.max(ret, dp[i]);
}
return ret;
}
}