连续
[leetcode 简单 滑动窗口] 674. 最长连续递增序列
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] depth = new int[]{15522, 13267, 7072, 23658, 4780, 5093, 954, 21401, 30560, 5332};
find(depth.length,depth);
}
public static void find (int count,int[] depth){
//res =1是为了出现【2,2,2,2,2】 返回0 的情况
int res =1;
int c=1;
for(int i=0;i<count-1;i++){
if(depth[i]<depth[i+1]) {
c++;
res = Math.max(res,c);
}
else {
c = 1;
}
}
System.out.println(res);
}
}
不连续 ----动态规划
输出不连续的递增(递减)子数组的长度和子序列
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int[] depth = new int[]{15522, 13267, 7072, 23658, 4780, 5093, 954, 21401, 30560, 53322};
find(depth.length,depth);
}
public static void find (int count,int[] depth){
int[] dp = new int[count];
for (int i = 0; i < count; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (depth[j] < depth[i] && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
}
}
}
int max = 0;
int index = 0;
for (int i = 0; i < count; i++) {
if (dp[i] > max) {
max = dp[i];
index = i;
}
}
System.out.println(max);
int length = max;
// System.out.println(max + ":" + index);
int[] r = new int[max];
for (int i = index; i >= 0; i--) {
if (dp[i] == max) {
max = max - 1;
r[max] = depth[i];
}
}
for (int i = 0; i < length; i++) {
System.out.print(r[i] + " ");
}
}
}
[leetcode]673. 最长递增子序列的个数
题目描述
给定一个未排序的整数数组,找到最长递增子序列的个数。
示例 1:
输入: [1,3,5,4,7]
输出: 2
解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7]。
示例 2:
输入: [2,2,2,2,2]
输出: 5
解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。
注意: 给定的数组长度不超过 2000 并且结果一定是32位有符号整数。
通过次数12,941提交次数35,826
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
动态规划
class Solution {
public int findNumberOfLIS(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int n = nums.length;
int[] dp = new int[n];
int[] counter = new int[n];
Arrays.fill(dp, 1);
Arrays.fill(counter, 1);
int max = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
if (dp[j] + 1 > dp[i]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
counter[i] = counter[j];
} else if (dp[j] + 1 == dp[i]) {
counter[i] += counter[j];
}
}
}
max = Math.max(max, dp[i]);
}
int res = 0;
for (int i = 0; i < n; i++) {
if (dp[i] == max) res += counter[i];
}
return res;
}
}