300. 最长递增子序列
class Solution:def lengthOfLIS(self, nums: List[int]) -> int:length = len(nums)if length==1:return 1# dp[i]指的是以nums[i]为结尾的最长递增子序列的长度。dp = [1] * lengthmmax = 1for i in range(1, length):for j in range(i):if nums[i] > nums[j]:dp[i] = max(dp[j]+1,dp[i])mmax = max(dp[i], mmax)return mmax
674.最长连续递增序列
1.双指针
class Solution:def findLengthOfLCIS(self, nums: List[int]) -> int:length = len(nums)if length == 1:return 1# 双指针解法mmax = 1i = 0while i < length-1:temp = 1j = i + 1while j < length and nums[j]>nums[j-1]:temp += 1j += 1i = jmmax = max(mmax,temp)return mmax
效率:3ms,击败77.69%
2.动态规划
class Solution:def findLengthOfLCIS(self, nums: List[int]) -> int:length = len(nums)if length == 1:return 1mmax = 1dp = [1] * lengthfor i in range(1, length):if nums[i] > nums[i-1]:dp[i] = dp[i-1]+1mmax = max(dp[i], mmax)return mmax
效率:7ms,击败24.83%