ZhangZhihui's Blog  

Given an integer array nums, return the length of the longest strictly increasing .

 

Example 1:

Input: nums = [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.

Example 2:

Input: nums = [0,1,0,3,2,3]
Output: 4

Example 3:

Input: nums = [7,7,7,7,7,7,7]
Output: 1

 

Constraints:

  • 1 <= nums.length <= 2500
  • -104 <= nums[i] <= 104

 

My Solution:

class Solution:
    def lengthOfLIS(self, nums: List[int]) -> int:
        n = len(nums)
        if n == 1:
            return 1
        
        # LIS[k] = LIS ending at index k
        # LIS[n] = 1 + max{LIS[k] | k < n, nums[k] < nums[n]}
        LIS = [1] * n  
        for i in range(1, n):
            subproblems = [LIS[k] for k in range(i) if nums[k] < nums[i]]
            LIS[i] = 1 + max(subproblems, default=0)
        
        return max(LIS)

 

 

posted on 2025-04-08 19:38  ZhangZhihuiAAA  阅读(7)  评论(0)    收藏  举报