代码随想录训练营Day51| 300.最长递增子序列 674. 最长连续递增序列 718. 最长重复子数组

目录

学习目标

学习内容

 300.最长递增子序列 

 674. 最长连续递增序列 

  718. 最长重复子数组  


学习目标

  •  300.最长递增子序列 
  •  674. 最长连续递增序列 
  •  718. 最长重复子数组  

学习内容

 300.最长递增子序列 

300. 最长递增子序列 - 力扣(LeetCode)icon-default.png?t=N4N7https://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]

 674. 最长连续递增序列 

674. 最长连续递增序列 - 力扣(LeetCode)icon-default.png?t=N4N7https://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)icon-default.png?t=N4N7https://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

 

你可能感兴趣的:(leetcode,算法,职场和发展,数据结构,python)