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
相关推荐
尼尔森系3 小时前
排序与算法:希尔排序
c语言·算法·排序算法
AC使者4 小时前
A. C05.L08.贪心算法入门
算法·贪心算法
冠位观测者4 小时前
【Leetcode 每日一题】624. 数组列表中的最大距离
数据结构·算法·leetcode
yadanuof4 小时前
leetcode hot100 滑动窗口&子串
算法·leetcode
可爱de艺艺4 小时前
Go入门之函数
算法
武乐乐~5 小时前
欢乐力扣:旋转图像
算法·leetcode·职场和发展
a_j585 小时前
算法与数据结构(子集)
数据结构·算法·leetcode
清水加冰5 小时前
【算法精练】背包问题(01背包问题)
c++·算法
Ronin-Lotus6 小时前
蓝桥杯篇---IAP15F2K61S2串口
单片机·嵌入式硬件·职场和发展·蓝桥杯·c·iap15f2k61s2
慢一点会很快7 小时前
FRRouting配置与OSPF介绍,配置,命令,bfd算法:
算法·智能路由器·php·ospf