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
相关推荐
好易学·数据结构11 分钟前
可视化图解算法73:跳台阶(爬楼梯)
数据结构·算法·leetcode·动态规划·笔试
Salt_072812 分钟前
DAY32 类的定义和方法
开发语言·python·算法·机器学习
Tisfy17 分钟前
LeetCode 3433.统计用户被提及情况:(大)模拟
linux·算法·leetcode
一招定胜负26 分钟前
逻辑回归核心原理与实践指南
算法·逻辑回归·线性回归
长安er37 分钟前
LeetCode 98. 验证二叉搜索树 解题总结
java·数据结构·算法·leetcode·二叉树·力扣
薛不痒38 分钟前
机器学习算法之线性回归&逻辑回归
算法·机器学习·逻辑回归
sin_hielo39 分钟前
leetcode 3433
数据结构·算法·leetcode
Swift社区1 小时前
LeetCode 448 - 找到所有数组中消失的数字
算法·leetcode·职场和发展
OKkankan1 小时前
二叉搜索树
c语言·数据结构·c++·算法
茶猫_1 小时前
C++学习记录-旧题新做-字符串压缩
c语言·c++·学习·算法·leetcode