[leetcode 中等 动态规划]673. 最长递增子序列的个数 [leetcode 简单 滑动窗口] 674. 最长连续递增序列 [笔试]数组的递增子数组序列

连续

[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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值