674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).
Example 1:
Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3.
Even though [1,3,5,7] is also an increasing subsequence, it’s not a continuous one where 5 and 7 are separated by 4.
Example 2:
Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1.
Note: Length of the array will not exceed 10,000.
解题思路:
遍历数组,比较记录最大增长的序列的长度
时间复杂度 O(n)
也可以用动态规划去解题,时间复杂度为 O(n * n)
注意逻辑上的闭环
class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if nums == []:
return 0
# numlen = [-1 for i in range(len(nums))]
numLen1, numLen2 = 1, 1
for i in range(len(nums) - 1):
if nums[i] < nums[i+1]:
numLen1 += 1
elif numLen2 < numLen1:
numLen2 = numLen1
numLen1 = 1
else:
numLen1 = 1
# print(numLen1,numLen2)
if numLen2 < numLen1:
return numLen1
return numLen2
请多多指教。
文章标题:674. Longest Continuous Increasing Subsequence
本文作者:顺强
发布时间:2020-04-29, 23:59:00
原始链接:http://shunqiang.ml/leetcode-674-longest-continues-increasing-subsequence/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。