leetcode#674 Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest
continuousincreasing subsequence.Example 1:
123456 >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:
12345 >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.
解释
给定一个无序的整数数组,找到并返回其中长度最长的且依次递增的子序列。
我的解法
从头到尾,依次比较每一对相邻元素,如果一直满足从大到小的关系,那么当前计数值就累加,否则就先判断当前计数值与全局计数值的大小关系,决定是否替换全局计数值,不论大小关系如何,都将当前计数值置为0,开始下次计数过程。
|
|
大神解法
解法一:思路与我的解法类似。
|
|
解法二:动态规划。
虽然使用的DP,但是基本的思想也是类似的:创建一个与给定数组大小相同的数组,每一个位置保存目前位置的子序列长度(大小连续的长度计数),同样,如果不符合连续增长的大小关系,那么将该位置的值置为1,重新开始计数。
|
|