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
相关推荐
雲墨款哥18 分钟前
JS算法练习-Day10-判断单调数列
前端·javascript·算法
FPGA22 分钟前
CRC校验原理及其FPGA实现
算法
Jina AI29 分钟前
回归C++: 在GGUF上构建高效的向量模型
人工智能·算法·机器学习·数据挖掘·回归
程序员曦曦30 分钟前
10:00开始面试,10:06就出来了,问的问题有点变态。。。
自动化测试·软件测试·功能测试·程序人生·面试·职场和发展
Coovally AI模型快速验证34 分钟前
YOLO、DarkNet和深度学习如何让自动驾驶看得清?
深度学习·算法·yolo·cnn·自动驾驶·transformer·无人机
luoqice40 分钟前
linux下找到指定目录下最新日期log文件
linux·算法
楽码1 小时前
底层技术SwissTable的实现对比
数据结构·后端·算法
fffcccc11121 小时前
初级背包问题,层层剖析为什么这样做。最好需要自己推演一遍。
算法
瓦特what?2 小时前
关于C++的#include的超超超详细讲解
java·开发语言·数据结构·c++·算法·信息可视化·数据挖掘
楽码3 小时前
自动修复GoVet:语言实现对比
后端·算法·编程语言