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
相关推荐
踩坑记录14 分钟前
leetcode hot100 54.螺旋矩阵 medium
leetcode
wzf@robotics_notes16 分钟前
振动控制提升 3D 打印机器性能
嵌入式硬件·算法·机器人
机器学习之心34 分钟前
MATLAB基于多指标定量测定联合PCA、OPLS-DA、FA及熵权TOPSIS模型的等级预测
人工智能·算法·matlab·opls-da
Loo国昌43 分钟前
【LangChain1.0】第八阶段:文档处理工程(LangChain篇)
人工智能·后端·算法·语言模型·架构·langchain
xb11321 小时前
Winforms实战项目:运动控制界面原型
算法
MicroTech20251 小时前
微算法科技(NASDAQ :MLGO)量子安全哈希(QSHA),增强量子时代的区块链安全保障
科技·算法·安全
高洁011 小时前
数字孪生与数字样机的技术基础:建模与仿真
python·算法·机器学习·transformer·知识图谱
不忘不弃1 小时前
模拟内存分配器2
算法
被星1砸昏头1 小时前
C++中的享元模式
开发语言·c++·算法
淡忘旧梦2 小时前
词错误率/WER算法讲解
人工智能·笔记·python·深度学习·算法