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
相关推荐
老蒋新思维15 小时前
反脆弱性设计:创始人IP与AI智能体如何构建愈动荡愈强大的知识商业|创客匠人
人工智能·网络协议·tcp/ip·算法·机器学习·创始人ip·创客匠人
Salt_072815 小时前
DAY 36 官方文档的阅读
python·算法·机器学习·github
明洞日记16 小时前
【VTK手册027】VTK 颜色连续映射:vtkColorTransferFunction 深度解析与实战指南
c++·图像处理·算法·vtk·图形渲染
B_lack02617 小时前
西门子PLC结构化编程_线性插值算法功能块
算法·pid·西门子plc·博途·线性插值·开环控制
fufu031117 小时前
Linux环境下的C语言编程(四十三)
linux·c语言·算法
业精于勤的牙17 小时前
三角形最小路径和(二)
算法
风筝在晴天搁浅17 小时前
hot100 239.滑动窗口最大值
数据结构·算法·leetcode
夏乌_Wx18 小时前
练题100天——DAY31:相对名次+数组拆分+重塑矩阵
数据结构·算法
LYFlied18 小时前
【算法解题模板】-解二叉树相关算法题的技巧
前端·数据结构·算法·leetcode
Ven%18 小时前
【AI大模型算法工程师面试题解析与技术思考】
人工智能·python·算法