原文链接: https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence
英文原文
Given an unsorted array of integers nums
, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.
A continuous increasing subsequence is defined by two indices l
and r
(l < r
) such that it is [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]]
and for each l <= i < r
, nums[i] < nums[i + 1]
.
Example 1:
Input: nums = [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3. Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element 4.
Example 2:
Input: nums = [2,2,2,2,2] Output: 1 Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly increasing.
Constraints:
1 <= nums.length <= 104
-109 <= nums[i] <= 109
中文题目
给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
连续递增的子序列 可以由两个下标 l
和 r
(l < r
)确定,如果对于每个 l <= i < r
,都有 nums[i] < nums[i + 1]
,那么子序列 [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]]
就是连续递增子序列。
示例 1:
输入:nums = [1,3,5,4,7] 输出:3 解释:最长连续递增序列是 [1,3,5], 长度为3。 尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为 5 和 7 在原数组里被 4 隔开。
示例 2:
输入:nums = [2,2,2,2,2] 输出:1 解释:最长连续递增序列是 [2], 长度为1。
提示:
1 <= nums.length <= 104
-109 <= nums[i] <= 109
通过代码
高赞题解
解题思路
- 标签:遍历
- 过程:
- count 为当前元素峰值,ans为最大峰值
- 初始化 count = 1
- 从 0 位置开始遍历,遍历时根据前后元素状态判断是否递增,递增则 count++,递减则 count=1
- 如果 count>ans,则更新 ans
- 直到循环结束
- 时间复杂度:$O(N)$
代码
class Solution {
public int findLengthOfLCIS(int[] nums) {
if(nums.length <= 1)
return nums.length;
int ans = 1;
int count = 1;
for(int i=0;i<nums.length-1;i++) {
if(nums[i+1] > nums[i]) {
count++;
} else {
count = 1;
}
ans = count > ans ? count : ans;
}
return ans;
}
}
画解
<,,,,,,,,,,,,,,>
想看大鹏画解更多高频面试题,欢迎阅读大鹏的 LeetBook:《画解剑指 Offer 》,O(∩_∩)O
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
97897 | 195265 | 50.1% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|
相似题目
题目 | 难度 |
---|---|
最长递增子序列的个数 | 中等 |
最小窗口子序列 | 困难 |