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
相关推荐
少许极端几秒前
算法奇妙屋(三十四)-贪心算法学习之路 1
学习·算法·贪心算法
兑生3 分钟前
【灵神题单·贪心】3010. 将数组分成最小总代价的子数组 I | Java
java·开发语言·算法
垫脚摸太阳10 分钟前
二分查找经典算法题--数的范围
数据结构·算法
噜啦噜啦嘞好11 分钟前
算法篇:二分查找
数据结构·c++·算法·leetcode
setmoon21411 分钟前
C++中的构建器模式
开发语言·c++·算法
2301_8154829311 分钟前
C++中的桥接模式变体
开发语言·c++·算法
yunyun3212312 分钟前
C++与量子计算模拟
开发语言·c++·算法
吴声子夜歌13 分钟前
JavaScript——数组
java·javascript·算法
中小企业实战军师刘孙亮17 分钟前
农贸批发市场招商难?从卖摊位变经营赋能破局-佛山鼎策创局破局增长咨询
职场和发展·新媒体运营·创业创新·需求分析·内容运营
不知名。。。。。。。。18 分钟前
仿muduo库实现高并发服务器----HttpServer
运维·服务器·算法