[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
相关文章推荐
- lintcode——最长上升连续子序列
- lintCode刷题--最长上升连续子序列
- Lintcode最长上升连续子序列
- lintcode-397-最长上升连续子序列
- lintcode-最长上升连续子序列-397
- 最长上升连续子序列(LintCode)
- LintCode:最长上升连续子序列
- LintCode 最长上升连续子序列
- LintCode(easy)最长上升连续子序列
- lintcode longest-increasing-continuous-subsequence 最长上升连续子序列
- 最长上升连续子序列
- 经典字符串算法 “最长上升子序列,最大连续子序列和,最长公共子串”
- LintCode刷题——最长上升连续子序列
- 最长上升连续子序列(解题代码)
- lintcode 最长上升连续子序列 II(二维最长上升连续序列)
- [LintCode 124] 最长连续序列(Python)
- LintCode: 最长上升连续子序列
- “最长上升子序列,最大连续子序列和,最长公共子串”的Java实现
- LintCode- 最长上升连续子序列
- python_lintcode_397最长上升连续子序列_56两数之和