Leetcode 673. Number of Longest Increasing Subsequence

Problem

Given an integer array nums, return the number of longest increasing subsequences.

Notice that the sequence has to be strictly increasing.

Algorithm

Dynamic Programming (DP). Use to lists dp_L[i] and dp_N[i] to save the length and size of longest increasing subsequences of the first i items. Then sum all the items with the longest length.

Code

python3 复制代码
class Solution:
    def findNumberOfLIS(self, nums: List[int]) -> int:
        nlen = len(nums)
        dp_L = [1] * nlen
        dp_N = [1] * nlen
        for i in range(1, nlen):
            dp_L[i] = 1
            dp_N[i] = 1
            for j in range(i):
                if nums[j] < nums[i]:
                    if dp_L[i] == dp_L[j] + 1:
                        dp_N[i] += dp_N[j]
                    if dp_L[i] <= dp_L[j]:
                        dp_L[i] = dp_L[j] + 1
                        dp_N[i] =  dp_N[j]
        
        ans = 0
        max_l = max(dp_L)
        for i in range(nlen):
            if max_l == dp_L[i]:
                ans += dp_N[i]
        return ans
相关推荐
炽烈小老头6 分钟前
【每天学习一点算法 2026/03/29】搜索二维矩阵 II
学习·算法·矩阵
靴子学长13 分钟前
Qwen3.5 架构手撕源码
算法·架构·大模型
寒月小酒16 分钟前
3.28 OJ
算法
打瞌睡的朱尤23 分钟前
3.25蓝桥杯训练
职场和发展·蓝桥杯
AI成长日志25 分钟前
【笔面试算法学习专栏】堆与优先队列专题:数组中的第K个最大元素与前K个高频元素
学习·算法·面试
雅俗共赏1001 小时前
医学图像重建中常用的正则化分类
算法
IronMurphy1 小时前
【算法三十二】
算法
Mr_Xuhhh1 小时前
LeetCode 热题 100 刷题笔记:高频面试题详解(215 & 347)
算法·leetcode·排序算法
mmz12071 小时前
贪心算法3(c++)
c++·算法·贪心算法
j_xxx404_1 小时前
蓝桥杯基础--排序模板合集II(快速,归并,桶排序)
数据结构·c++·算法·蓝桥杯·排序算法