List: 300.最长递增子序列,674. 最长连续递增序列,718. 最长重复子数组
300.最长递增子序列longest-increasing-subsequence,674. 最长连续递增序列longest-continuous-increasing-subsequence,718. 最长重复子数组maximum-length-of-repeated-subarray
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
dp = [1] * len(nums)
for i in range(len(nums)):
for j in range(i):
if nums[i] > nums[j]:
dp[i] = max(dp[i], dp[j] + 1)
return max(dp)
class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
dp = [1] * len(nums)
for i in range(1, len(nums)):
if nums[i] > nums[i - 1]:
dp[i] = dp[i - 1] + 1
return max(dp)
class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
dp = [[0] * (len(nums2) + 1) for _ in range(len(nums1) + 1)] # 注意行列顺序
result = 0
for i in range(1, len(nums1) + 1):
for j in range(1, len(nums2) + 1):
if nums1[i - 1] == nums2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
if dp[i][j] > result:
result = dp[i][j]
return result