原題
給定一個整數(shù)數(shù)組(下標從 0 到 n-1锐借, n 表示整個數(shù)組的規(guī)模)障本,請找出該數(shù)組中的最長上升連續(xù)子序列棋电。(最長上升連續(xù)子序列可以定義為從右到左或從左到右的序列栗涂。)
樣例
給定 [5, 4, 2, 1, 3], 其最長上升連續(xù)子序列(LICS)為[5, 4, 2, 1], 返回 4.
給定 [5, 1, 2, 3, 4], 其最長上升連續(xù)子序列(LICS)為[1, 2, 3, 4], 返回 4.
解題思路
- 正序逆序各求一個最長序列的值摇锋,取最大
- 一個全局變量res記錄最長的長度丹拯,一個局部變量temp
- 當nums[i] > nums[i - 1]時站超,temp加1
- 當nums[i] <= nums[i - 1]時,temp重置為1
完整代碼
class Solution:
# @param {int[]} A an array of Integer
# @return {int} an integer
def longestIncreasingContinuousSubsequence(self, A):
# Write your code here
return max(self.helper(A), self.helper(A[::-1]))
def helper(self, nums):
res, temp = 0, 0
for i in range(len(nums)):
if nums[i] > nums[i - 1] or i == 0:
temp += 1
res = max(res, temp)
else:
temp = 1
return res