目录
300.最长递增子序列
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
if len(nums) <= 1:
return len(nums)
dp = [1] * len(nums)
res = 0
for i in range(1, len(nums)):
for j in range(0, i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)
res = max(res, dp[i]) #取长的子序列
return res
674. 最长连续递增序列
class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
if not nums:
return 0
n = len(nums)
dp = [1] * n
res = 1
for i in range(1, len(nums)):
if nums[i-1] < nums[i]:
dp[i] = dp[i-1] + 1
res = max(res, dp[i])
return res
718. 最长重复子数组
class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
m, n = len(nums1), len(nums2)
dp = [[0] * (n+1) for _ in range(m+1)]
res = 0
for i in range(1, m+1):
for j in range(1, n+1):
if nums1[i-1] == nums2[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
res = max(res, dp[i][j])
return res