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
相关推荐
文丑颜不良啊1 小时前
LeetCode1941~1960题目解析
java·leetcode
MUSHANGXS3 小时前
大模型时代的新蓝海-基于OS底层的AI-Agent应用
人工智能·职场和发展·交互
老师我太想进步了20264 小时前
【数据结构-4】算法美学之顺序表
数据结构·算法
风影同志783245 小时前
编码器是激活函数吗?—— 一个看起来很像、其实不是的类比
算法
UIU1145 小时前
P1481 魔族密码
c++·算法·动态规划
文丑颜不良啊6 小时前
LeetCode1961~1980题目解析
java·leetcode
文丑颜不良啊6 小时前
LeetCode2001~2020题目解析
java·leetcode
加宝的起点雷达8 小时前
40 条候选里 38 条来自同一个源,主卡整屏重样
人工智能·算法·创业
秋天的怀念_David8 小时前
算法一千零一夜 · 第二十六夜 协议的母语
java·spring boot·算法