674. Longest Continuous Increasing Subsequence #
Problem #
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:
0 <= nums.length <= 10^410^9 <= nums[i] <= 10^9
Problem Summary #
Given an unsorted integer array, find the longest continuous increasing subsequence and return the length of that sequence. A continuous increasing subsequence can be determined by two indices l and r (l < r). If for every l <= i < r, nums[i] < nums[i + 1], then the subsequence [nums[l], nums[l + 1], …, nums[r - 1], nums[r]] is a continuous increasing subsequence.
Solution Approach #
- Easy problem. This problem is different from Problem 128. This problem requires the subsequence to have continuous indices, so it becomes simpler. Scan the array once, record the length of the continuous increasing sequence, dynamically maintain this maximum value, and finally output it.
Code #
package leetcode
func findLengthOfLCIS(nums []int) int {
if len(nums) == 0 {
return 0
}
res, length := 1, 1
for i := 1; i < len(nums); i++ {
if nums[i] > nums[i-1] {
length++
} else {
res = max(res, length)
length = 1
}
}
return max(res, length)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}