Leetcode 300. Longest Increasing Subsequence

Problem

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

Algorithm

Dynamic Programming (DP). State: dp[i] is the longest strictly increasing subsequence length that use nums[i] as the last nums. dp[i] = max(dp[j]+1): nums[j] < nums[i].

Code

class Solution:
    def lengthOfLIS(self, nums: List[int]) -> int:
        nlen = len(nums)
        dp = [1] * nlen
        for i in range(1, nlen):
            for j in range(i):
                if nums[j] < nums[i] and dp[i] <= dp[j]:
                    dp[i] = dp[j] + 1

        return max(dp)

你可能感兴趣的:(Leetcode,解题报告,leetcode,算法,解题报告)