longest_increasing_subsequence

  • 2022-12-14
  • 浏览 (543)

longest_increasing_subsequence.py 源码

from typing import List

# 最长上升子序列

class Solution:

    # 动态规划
    def lengthOfLIS(self, nums: List[int]) -> int:
        if len(nums) <= 1:
            return len(nums)
            
        dp, res = [1] * len(nums), 0
        for i in range(1, len(nums)):
            for j in range(i):
                if nums[i] > nums[j]:
                    dp[i] = max(dp[i], dp[j] + 1)
            res = max(res, dp[i])
        return res

你可能感兴趣的文章

coin_change

decode_ways

edit_distance

0  赞