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
相关推荐
天选之女wow13 小时前
【代码随想录算法训练营——Day60】图论——94.城市间货物运输I、95.城市间货物运输II、96.城市间货物运输III
android·算法·图论
Blossom.11813 小时前
大模型在边缘计算中的部署挑战与优化策略
人工智能·python·算法·机器学习·边缘计算·pygame·tornado
时间醉酒13 小时前
数据结构:双向链表-从原理到实战完整指南
c语言·数据结构·算法
京东零售技术13 小时前
当搜索遇见 AIGC:京东零售的“千人千面”素材生成实践
算法
好学且牛逼的马13 小时前
【HOT100|1 LeetCode 1. 两数之和】
数据结构·算法·leetcode
Nebula_g13 小时前
C语言应用实例:斐波那契数列与其其他应用
c语言·开发语言·后端·学习·算法
不穿格子的程序员13 小时前
从零开始刷算法-单调栈-每日温度
算法·单调栈
麦烤楽鸡翅14 小时前
挡住洪水 (牛客)
java·数据结构·c++·python·算法·bfs·牛客
MicroTech202514 小时前
微算法科技(NASDAQ MLGO)采用动态层次管理和位置聚类技术,修改pBFT算法以提高私有区块链网络运行效率
科技·算法·聚类
~~李木子~~14 小时前
五子棋项目Alpha-Beta剪枝与MCTS+神经网络实现人机对弈算法对比报告
神经网络·算法·剪枝