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
相关推荐
YXXY31311 分钟前
模拟算法的介绍
算法
happymaker062634 分钟前
简单LRU的实现(基于LinkedHashMap)
算法·leetcode·lru
那我掉的头发算什么37 分钟前
【面试八股】一篇文章讲清楚JVM面试常考
jvm·面试·职场和发展·java虚拟机
冬天vs不冷38 分钟前
面试必知必会(13):MySQL锁机制
mysql·面试·职场和发展
华夏之光永存38 分钟前
独家:国家级光刻机项目架构师面试对话实录
面试·职场和发展
KNeeg_39 分钟前
黑马点评完整代码(RabbitMQ优化)+简历编写+面试重点 ⭐
java·redis·后端·spring·面试·职场和发展·黑马点评
FPGA小迷弟40 分钟前
FPGA工程师常见面试问题,有参考答案,必学!!!
fpga开发·面试·职场和发展·verilog·fpga·modelsim
Java后端的Ai之路40 分钟前
以为AI开发就是调接口?一场25K的面试让我看到真相,原来真正的技术深度在这!
人工智能·面试·职场和发展·agent·ai应用开发
会编程的土豆1 小时前
【数据结构与算法】空间复杂度从入门到面试:不仅会算,还要会解释
数据结构·c++·算法·面试·职场和发展
普通网友1 小时前
《算法面试必刷:15 个高频 LeetCode 题(附代码)》
算法·leetcode·面试