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
相关推荐
技术狂人1683 分钟前
(七)大模型工程落地与部署 10 题!vLLM/QPS 优化 / 高可用,面试实战必备(工程篇)
人工智能·深度学习·面试·职场和发展·vllm
im_AMBER10 分钟前
Leetcode 95 分割链表
数据结构·c++·笔记·学习·算法·leetcode·链表
Boilermaker199219 分钟前
[算法基础] FooldFill(DFS、BFS)
算法·深度优先·宽度优先
leiming625 分钟前
c++ find 算法
算法
CoovallyAIHub27 分钟前
YOLOv12之后,AI在火场如何进化?2025最后一篇YOLO论文揭示:要在浓烟中看见关键,仅靠注意力还不够
深度学习·算法·计算机视觉
梭七y28 分钟前
【力扣hot100题】(121)反转链表
算法·leetcode·链表
qq_4335545430 分钟前
C++字符串hash
c++·算法·哈希算法
无限进步_30 分钟前
【C语言】堆(Heap)的数据结构与实现:从构建到应用
c语言·数据结构·c++·后端·其他·算法·visual studio
再难也得平30 分钟前
两数之和和字母异位词分组
数据结构·算法
shizhenshide44 分钟前
极速响应:平均破解速度<3秒的验证码服务,为抢购爬虫而生
算法