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
相关推荐
Wilber的技术分享2 小时前
【机器学习实战笔记 12】集成学习:AdaBoost算法
人工智能·笔记·算法·决策树·机器学习·分类·集成学习
金融小师妹3 小时前
基于LSTM-GARCH混合模型的“获利了结”量化解析:黄金单日1.27%跌幅的技术性归因
大数据·人工智能·算法
缘友一世4 小时前
创业知识概论
经验分享·笔记·职场和发展·创业创新
天才测试猿4 小时前
软件测试环境搭建及测试过程
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
不良手残5 小时前
Java实现10大经典排序算法
数据结构·算法·排序算法
是紫焅呢5 小时前
I排序算法.go
开发语言·后端·算法·golang·排序算法·学习方法·visual studio code
辉辉还没睡5 小时前
Lora训练
人工智能·算法·机器学习
电院大学僧7 小时前
初学python的我开始Leetcode题10-2
python·算法·leetcode
Memories off7 小时前
小论文投稿记录
职场和发展
码破苍穹ovo8 小时前
二分查找----1.搜索插入位置
数据结构·算法