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)