您的位置:首页 > 编程语言 > Python开发

[Lintcode 397] 最长上升连续子序列(Python)

2017-08-11 18:05 337 查看

题目描述

给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)

样例

给定 [5, 4, 2, 1, 3], 其最长上升连续子序列(LICS)为 [5, 4, 2, 1], 返回 4.

给定 [5, 1, 2, 3, 4], 其最长上升连续子序列(LICS)为 [1, 2, 3, 4], 返回 4.

代码如下

class Solution:
"""
@param: : An array of Integer
@return: an integer
"""

def longestIncreasingContinuousSubsequence(self, A):
# write your code here
if A is None or len(A) == 0:
return 0
max_cnt = 1
# 递增序列
i = 1
cnt = 1
while i < len(A):
while i < len(A) and A[i] > A[i - 1]:
cnt += 1
i += 1
if cnt > max_cnt:
max_cnt = cnt
cnt = 1
i += 1
# 递减序列
i = 1
cnt = 1
while i < len(A):
while i < len(A) and A[i] < A[i - 1]:
cnt += 1
i += 1
if cnt > max_cnt:
max_cnt = cnt
cnt = 1
i += 1
return max_cnt
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: