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
相关推荐
不知名XL3 分钟前
day24 贪心算法 part02
算法·贪心算法
AI科技星9 分钟前
时空几何:张祥前统一场论20核心公式深度总结
人工智能·线性代数·算法·机器学习·生活
菜鸟233号13 分钟前
力扣518 零钱兑换II java实现
java·数据结构·算法·leetcode·动态规划
咋吃都不胖lyh1 小时前
Haversine 距离算法详解(零基础友好版)
线性代数·算法·机器学习
FPGA小c鸡1 小时前
FPGA通信基带算法完全指南:从理论到实战的DSP加速方案
算法·fpga开发
@Aurora.1 小时前
优选算法【专题三:二分查找算法】
算法
soldierluo1 小时前
向量与向量数据
人工智能·算法·机器学习
a努力。1 小时前
字节跳动Java面试被问:一致性哈希的虚拟节点和数据迁移
java·开发语言·分布式·算法·缓存·面试·哈希算法
VT.馒头2 小时前
【力扣】2622. 有时间限制的缓存
javascript·算法·leetcode·缓存·typescript