[LeetCode] 674. 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]] 就是连续递增子序列。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/longest-continuous-increasing-subsequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
因为题目要求这个递增序列要是连续的所以思路就是比较相邻数组之间的大小关系即可。
时间O(n)
空间O(1)
Java实现
1 class Solution { 2 public int findLengthOfLCIS(int[] nums) { 3 // corner case 4 if (nums == null || nums.length == 0) { 5 return 0; 6 } 7 8 // normal case 9 int res = 1; 10 int count = 1; 11 for (int i = 1; i < nums.length; i++) { 12 if (nums[i] > nums[i - 1]) { 13 count++; 14 res = Math.max(res, count); 15 } else { 16 count = 1; 17 } 18 } 19 return res; 20 } 21 }