欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lintcode:最长上升连续子序列

程序员文章站 2022-03-24 17:38:26
...

lintcode:最长上升连续子序列

class Solution:
    """
    @param: A: An array of Integer
    @return: an integer
    """
    def longestIncreasingContinuousSubsequence(self, A):
        # write your code here

        if len(A) < 3:
            return len(A)

        # 一直升序排列    
        flag, count_1 = 1, 0
        for i in range(1, len(A)):
            if A[i] > A[i - 1]:
                flag += 1
            else:
                count_1 = max(count_1, flag)
                flag = 1
        count_1 = max(count_1, flag)

        # 一直降序排列
        flag, count_2 = 1, 0
        for j in range(1, len(A)):
            if A[j] < A[j - 1]:
                flag += 1
                count_2 = max(count_2, flag)
            else:
                count_2 = max(count_2, flag)
                flag = 1
        count_2 = max(count_2, flag)

        return max(count_1, count_2)