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
相关推荐
CYLAM20254 分钟前
STC32G12K128单片机实现高精度算法及常用初等函数值的计算
算法
奶人五毛拉人一块13 分钟前
动态规划--子数组类型
算法·动态规划·子数组问题
HugoStudio_SWAN17 分钟前
洛谷 P1321 单词覆盖还原——从蜡板上的密信到数字水印
c++·学习·程序人生·算法
刘孬孬沉迷学习1 小时前
AI音频领域完整调研:任务、算法、大模型研究全梳理
人工智能·算法·音视频
退休倒计时1 小时前
【每日五题】leetcode TypeScript
算法·leetcode·职场和发展·typescript
机器学习之心1 小时前
基于BiGRU-Attention的轴承剩余寿命预测(MATLAB实现):从振动信号到RUL曲线的完整闭环
数据结构·算法·matlab·轴承剩余寿命预测·振动信号·bigru-attention
青梅橘子皮1 小时前
优选算法---专题2(滑动窗口)
数据结构·算法
程序猫.1 小时前
双指针问题
java·数据结构·算法
心抵鹊2 小时前
力扣每日一题:计算右侧小于当前元素的个数(hard)
算法·leetcode
鹿角片ljp2 小时前
LeetCode 142:环形链表 II |HashSet 保底解 + Floyd 快慢指针找环入口
算法·leetcode·链表