力扣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结尾的子序列中,最长递增子序列的个数
相关推荐
如竟没有火炬3 分钟前
全排列——交换的思想
开发语言·数据结构·python·算法·leetcode·深度优先
寂静山林16 分钟前
UVa 12526 Cellphone Typing
算法
kyle~1 小时前
C++---嵌套类型(Nested Types)封装与泛型的基石
开发语言·c++·算法
sali-tec1 小时前
C# 基于halcon的视觉工作流-章48-短路断路
开发语言·图像处理·人工智能·算法·计算机视觉
墨染点香2 小时前
LeetCode 刷题【128. 最长连续序列】
算法·leetcode·职场和发展
被AI抢饭碗的人2 小时前
算法题(240):最大食物链计数
算法
熬了夜的程序员2 小时前
【LeetCode】82. 删除排序链表中的重复元素 II
数据结构·算法·leetcode·链表·职场和发展·矩阵·深度优先
欧克小奥2 小时前
Floyd判圈算法(Floyd Cycle Detection Algorithm)
算法·floyd
熬了夜的程序员3 小时前
【LeetCode】83. 删除排序链表中的重复元素
算法·leetcode·链表
胖咕噜的稞达鸭3 小时前
AVL树手撕,超详细图文详解
c语言·开发语言·数据结构·c++·算法·visual studio