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_Li and dp_Ni 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
相关推荐
hanlin036 分钟前
刷题笔记:力扣第32题-最长有效括号
笔记·算法·leetcode
All for pursuit.27 分钟前
【动态规划-21】312.戳气球
数据结构·c++·算法·leetcode·动态规划
碧海银沙音频科技研究院3 小时前
基于炬芯ATS3089X自研AEC声学回声消除算法
算法
通问AI3 小时前
在两台 Mac Studio 上跑 800B 模型:MLX + RDMA over Thunderbolt 实测与四道门选型法
算法
ZywOo6233 小时前
《算法竞赛进阶指南》0x03 前缀和与差分
算法
Scintilla_y3 小时前
DAY2 滑动窗口
算法·leetcode·散列表
lvwangshu3 小时前
笛卡尔树
算法·图论
026d234 小时前
状态信号只知“其然”不知“其所以然”:变工况机械故障诊断的物理盲区
算法
姜鱼问生4 小时前
备份和服务器在同一台机器上,等于没备份
算法·架构
姜鱼问生4 小时前
别再 chmod 777 了,用 ACL 给它开一扇窗
后端·算法·架构