目录
学习目标
学习内容
300.最长递增子序列
674. 最长连续递增序列
718. 最长重复子数组
学习目标
- 300.最长递增子序列
- 674. 最长连续递增序列
- 718. 最长重复子数组
学习内容
300.最长递增子序列
300. 最长递增子序列 - 力扣(LeetCode)https://leetcode.cn/problems/longest-increasing-subsequence/
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
n = len(nums)
dp = [1]*(n)
for i in range(1,n):
for j in range(0,i):
if nums[i]>nums[j]:
dp[i] = max(dp[i],dp[j]+1)
return max(dp)
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
n = len(nums)
dp = [-10005]
dp[0] = nums[0]
res = 1
for i in range(1,n):
if dp[res-1]<nums[i]:
dp.append(nums[i])
res+=1
else:
left = 0
right = res-1
while left<=right:
mid = left+(right-left)//2
if dp[mid]<nums[i]:
left = mid+1
else:
right = mid-1
dp[left] = nums[i]
#print(dp)
return res
674. 最长连续递增序列
674. 最长连续递增序列 - 力扣(LeetCode)https://leetcode.cn/problems/longest-continuous-increasing-subsequence/
class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
res = 1
tmp = 1
n = len(nums)
for i in range(n-1):
if nums[i+1]>nums[i]:
tmp+=1
res = max(res,tmp)
else:
tmp = 1
return res
718. 最长重复子数组
718. 最长重复子数组 - 力扣(LeetCode)https://leetcode.cn/problems/maximum-length-of-repeated-subarray/
class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
m = len(nums2)
res = 0
dp = [[0]*(n+1)for _ in range(m+1)]
for i in range(1,m+1):
for j in range(1,n+1):
if nums1[j-1]==nums2[i-1]:
dp[i][j]=dp[i-1][j-1]+1
res = max(res,dp[i][j])
return res