Problem

Given an unsorted array of integers nums, return the length of the longestcontinuous 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].

Examples

Example 1

1
2
3
4
5
6
7
8
9

    
    
    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

1
2
3
4
5
6
7
8

    
    
    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 <= 10^4
  • -109 <= nums[i] <= 10^9

Solution

Method 1 – Single Pass Linear Scan (1)

Intuition

We can find the longest continuous increasing subsequence by scanning the array once, keeping track of the current increasing segment and updating the maximum length found so far.

Approach

  1. Initialize two variables: ans for the maximum length and cur for the current segment length.
  2. Iterate through the array:
    • If the current number is greater than the previous, increment cur.
    • Otherwise, reset cur to 1.
    • Update ans if cur is greater.
  3. Return ans as the result.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        int ans = 1, cur = 1;
        for (int i = 1; i < nums.size(); ++i) {
            if (nums[i] > nums[i-1]) cur++;
            else cur = 1;
            ans = max(ans, cur);
        }
        return ans;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
func findLengthOfLCIS(nums []int) int {
    ans, cur := 1, 1
    for i := 1; i < len(nums); i++ {
        if nums[i] > nums[i-1] {
            cur++
        } else {
            cur = 1
        }
        if cur > ans {
            ans = cur
        }
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    public int findLengthOfLCIS(int[] nums) {
        int ans = 1, cur = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > nums[i-1]) cur++;
            else cur = 1;
            ans = Math.max(ans, cur);
        }
        return ans;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    fun findLengthOfLCIS(nums: IntArray): Int {
        var ans = 1
        var cur = 1
        for (i in 1 until nums.size) {
            if (nums[i] > nums[i-1]) cur++
            else cur = 1
            ans = maxOf(ans, cur)
        }
        return ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution:
    def findLengthOfLCIS(self, nums: list[int]) -> int:
        ans = cur = 1
        for i in range(1, len(nums)):
            if nums[i] > nums[i-1]:
                cur += 1
            else:
                cur = 1
            ans = max(ans, cur)
        return ans
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
impl Solution {
    pub fn find_length_of_lcis(nums: Vec<i32>) -> i32 {
        let mut ans = 1;
        let mut cur = 1;
        for i in 1..nums.len() {
            if nums[i] > nums[i-1] {
                cur += 1;
            } else {
                cur = 1;
            }
            ans = ans.max(cur);
        }
        ans
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
    findLengthOfLCIS(nums: number[]): number {
        let ans = 1, cur = 1;
        for (let i = 1; i < nums.length; i++) {
            if (nums[i] > nums[i-1]) cur++;
            else cur = 1;
            ans = Math.max(ans, cur);
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of the array. We scan the array once.
  • 🧺 Space complexity: O(1), only a few variables are used.