public class DayCode {
public static void main(String[] args) {
int[][] num = new int[][]{{5, 4}, {6, 4}, {6, 7}, {2, 3}};
int ans = new DayCode().maxEnvelopes(num);
System.out.println("ans is " + ans);
}
/**
* https://leetcode-cn.com/problems/russian-doll-envelopes/submissions/
* @param envelopes
* @return
*/
public int maxEnvelopes(int[][] envelopes) {
int ans = 0;
Arrays.sort(envelopes, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[0] == o2[0]) {
return o2[1] - o1[1];
} else {
return o1[0] - o2[0];
}
}
});
int n = envelopes.length;
int[] heights = new int[n];
for (int i = 0; i < n; i++) {
heights[i] = envelopes[i][1];
}
ans = lengthOfLIS(heights);
return ans;
}
/**
* https://leetcode-cn.com/problems/longest-increasing-subsequence/
* @param nums
* @return
*/
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) {
return 0;
}
int ans = 1;
int[] dp = new int[nums.length];
Arrays.fill(dp, 1);
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
ans = Math.max(dp[i], ans);
}
return ans;
}
}
评论