Skip to content

Latest commit

 

History

History
42 lines (33 loc) · 1.24 KB

File metadata and controls

42 lines (33 loc) · 1.24 KB

673. 最长递增子序列的个数

给定一个未排序的整数数组 nums返回最长递增子序列的个数

注意 这个数列必须是 严格 递增的。

示例 1:

输入: nums = [1,3,5,4,7]
输出: 2
解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7]。

示例 2:

输入: nums = [2,2,2,2,2]
输出: 5
解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5。

提示:

  • 1 <= nums.length <= 2000
  • -106 <= nums[i] <= 106

题解 (Python)

1. 题解

class Solution:
    def findNumberOfLIS(self, nums: List[int]) -> int:
        dp = [[[10000001, 0], [-1000001, 1]]]

        for num in nums:
            if dp[-1][-1][0] < num:
                dp.append([[1000001, 0]])

            i = bisect.bisect_left(dp, num, key=lambda x: x[-1][0])
            j = bisect.bisect_left(dp[i - 1][::-1], num, key=lambda x: x[0])
            count = dp[i][-1][1] + dp[i - 1][-1][1] - dp[i - 1][-j - 1][1]
            dp[i].append([num, count])

        return dp[-1][-1][1]