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
相关推荐
曹牧15 小时前
C#:数字的定义和表示方式
算法·c#
Nil20815 小时前
leetcode 138随机链表的复制
算法·leetcode·链表
疯狂打码的少年17 小时前
【数据结构】图的遍历:深度优先搜索(DFS)
数据结构·笔记·算法·深度优先
-凌凌漆-18 小时前
【freertos】Task创建(v2)
java·开发语言·算法
Nil20818 小时前
leetcode 24两两交换链表中的节点
算法·leetcode·链表
.格子衫.19 小时前
033动态规划之状态压缩DP——算法备赛
算法·动态规划
ysa05103019 小时前
c++常用自带函数用法与注意
c++·笔记·算法
带多刺的玫瑰20 小时前
Leecode#4刷题之寻找两个正序数组的中位数
java·前端·算法
土司大王20 小时前
LeetCode hot100——除了自身以外数组的乘积
数据结构·算法·leetcode