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
相关推荐
Lzh编程小栈几秒前
数据结构与算法之队列深度解析:循环队列+C 语言硬核实现 + 面试考点全梳理
c语言·开发语言·汇编·数据结构·后端·算法·面试
AbandonForce3 分钟前
模拟实现vector
开发语言·c++·算法
少许极端8 分钟前
算法奇妙屋(四十二)-贪心算法学习之路 9
学习·算法·贪心算法
CoderCodingNo8 分钟前
【NOIP】1998真题解析 luogu-P1010 幂次方 | GESP四、五级以上可练习
算法
py有趣15 分钟前
力扣热门100题之最小覆盖子串
算法·leetcode
汀、人工智能17 分钟前
[特殊字符] 第102课:添加与搜索单词
数据结构·算法·均值算法·前缀树·trie·添加与搜索单词
汀、人工智能21 分钟前
07 - 字典dict:哈希表的Python实现
数据结构·算法·数据库架构·哈希表的python实现
oG99bh7CK27 分钟前
高光谱成像基础(六)滤波匹配 MF
人工智能·算法·目标跟踪
汀、人工智能27 分钟前
04 - 控制流:if/for/while
数据结构·算法·链表·数据库架构··if/for/while
努力学习的小廉1 小时前
我爱学算法之——动态规划(四)
算法·动态规划