力扣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;
    }
}

状态表示:

  • len[i]表示:以i结尾的子序列中,最长递增子序列的长度
  • count[i]表示:以i结尾的子序列中,最长递增子序列的个数
相关推荐
海梨花5 分钟前
CSP认证练习题目推荐 (1)
算法·深度优先·csp
天上的光33 分钟前
大模型——剪枝、量化、蒸馏、二值化
算法·机器学习·剪枝
pzx_0011 小时前
【LeetCode】14. 最长公共前缀
算法·leetcode·职场和发展
self_myth1 小时前
算法与数据结构实战技巧:从复杂度分析到数学优化
算法
songx_992 小时前
leetcode10(跳跃游戏 II)
数据结构·算法·leetcode
先做个垃圾出来………3 小时前
差分数组(Difference Array)
java·数据结构·算法
hansang_IR3 小时前
【题解】洛谷 P4286 [SHOI2008] 安全的航线 [递归分治]
c++·数学·算法·dfs·题解·向量·点积
乐迪信息3 小时前
乐迪信息:AI摄像机在智慧煤矿人员安全与行为识别中的技术应用
大数据·人工智能·算法·安全·视觉检测
多恩Stone3 小时前
【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
人工智能·python·算法·3d·aigc
dragoooon344 小时前
[数据结构——lesson5.1链表的应用]
数据结构·链表