最长连续不下降子序列:
private int getLongestNoDecrese2(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int[] nums2 = Arrays.copyOf(nums, nums.length);
Arrays.sort(nums2);
int n = nums.length;
int dp[][] = new int[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else if (nums[i - 1] == nums2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[n][n];
}
===============
private int getLongestNoDecrese2(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int dp[] = new int[nums.length];
dp[0] = 1;
int res = 1;
for (int i = 1; i < nums.length; i++) {
for (int j = i - 1; j >= 0; j--) {
if (nums[i] >= nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
for (int d : dp) {
res = Math.max(d, res);
}
return res;
}
没有评论:
发表评论