力扣No.673.最长递增子序列的个数

题目:

链接:

https://leetcode.cn/problems/number-of-longest-increasing-subsequence/description/

代码:

复制代码
class Solution {
    public int findNumberOfLIS(int[] nums) {
        int n=nums.length;
        int[] len=new int[n];
        int[] count=new int[n];
        //初始化
        for(int i=0;i<n;i++) len[i]=count[i]=1;
        int maxLen=0,maxCount=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<i;j++){
                //转移方程
                if(nums[i]>nums[j]){
                    if(len[i]==len[j]+1){
                        count[i]+=count[j];
                    }else if(len[i]<len[j]+1){
                        len[i]=len[j]+1;
                        count[i]=count[j];
                    }
                }
            }
            //返回值
            if(maxLen<len[i]){ 
                maxLen=len[i];
                maxCount=count[i];
            }else if(maxLen==len[i]){
                maxCount+=count[i];
            }
        }
        //返回结果
        return maxCount;
    }
}

状态表示:

  • leni表示:以i结尾的子序列中,最长递增子序列的长度
  • counti表示:以i结尾的子序列中,最长递增子序列的个数
相关推荐
洛水水几秒前
【力扣100题】81.寻找两个正序数组的中位数
数据结构·算法·leetcode
happymaker062630 分钟前
LeetCodeHot100——155.最小栈
算法
洛水水40 分钟前
【力扣100题】85.每日温度
算法·leetcode·职场和发展
Coder-magician44 分钟前
《代码随想录》刷题打卡day15:二叉树part05
数据结构·c++·算法
Kurisu_红莉栖1 小时前
力扣56合并区间
算法·leetcode
Darling噜啦啦1 小时前
二叉树与递归算法实战:从树结构到 LeetCode 爬楼梯,一文吃透前端数据结构与递归思维
前端·javascript·数据结构
Irissgwe1 小时前
算法的时间复杂度和空间复杂度
数据结构·c++·算法·c·时间复杂度·空间复杂度
随意起个昵称1 小时前
区间dp-基础题目3(永别)
c++·算法
周末也要写八哥1 小时前
有向图Hierholzer算法的另一种实现
算法
bIo7lyA8v1 小时前
算法调优中的性能回归与基准测试分析的技术8
算法·数据挖掘·回归