Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

674. Longest Continuous Increasing Subsequence - Leetcode Solution

Code Implementation

class Solution:
    def findLengthOfLCIS(self, nums):
        if not nums:
            return 0
        max_len = 1
        curr_len = 1
        for i in range(1, len(nums)):
            if nums[i] > nums[i-1]:
                curr_len += 1
                max_len = max(max_len, curr_len)
            else:
                curr_len = 1
        return max_len
      
class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        if (nums.empty()) return 0;
        int maxLen = 1, currLen = 1;
        for (int i = 1; i < nums.size(); ++i) {
            if (nums[i] > nums[i-1]) {
                currLen++;
                maxLen = max(maxLen, currLen);
            } else {
                currLen = 1;
            }
        }
        return maxLen;
    }
};
      
class Solution {
    public int findLengthOfLCIS(int[] nums) {
        if (nums.length == 0) return 0;
        int maxLen = 1, currLen = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > nums[i - 1]) {
                currLen++;
                maxLen = Math.max(maxLen, currLen);
            } else {
                currLen = 1;
            }
        }
        return maxLen;
    }
}
      
var findLengthOfLCIS = function(nums) {
    if (nums.length === 0) return 0;
    let maxLen = 1, currLen = 1;
    for (let i = 1; i < nums.length; i++) {
        if (nums[i] > nums[i - 1]) {
            currLen++;
            maxLen = Math.max(maxLen, currLen);
        } else {
            currLen = 1;
        }
    }
    return maxLen;
};
      

Problem Description

Given an array of integers nums, your task is to find the length of the longest continuous increasing subsequence (LCIS) in the array. A continuous increasing subsequence is a sequence of elements from nums that are strictly increasing and are also consecutive in the array (i.e., no elements are skipped).

  • The subsequence must be strictly increasing: each next number is greater than the previous one.
  • The subsequence must be continuous: elements are adjacent in the original array.
  • Return the length of the longest such subsequence. If there are multiple, return the length of any one of them.
  • If nums is empty, return 0.

Thought Process

To solve this problem, let's first think about what it means to have a "longest continuous increasing subsequence." We need to find the longest stretch in the array where each number is bigger than the one before it, and all numbers are next to each other.

The brute-force way would be to check every possible subarray, but that's inefficient, especially for large arrays. Instead, we can "scan" the array from left to right, keeping track of the current increasing streak. If the streak breaks (the next number is not larger), we reset our counter. Throughout, we keep track of the maximum streak we've seen so far.

This is similar to counting the longest run of heads in a sequence of coin tosses—you only need to remember the current run and the best run.

Solution Approach

Let's break down the steps to solve the problem efficiently:

  1. Initialize Counters:
    • Set a variable (e.g., maxLen) to keep track of the longest increasing subsequence found so far. Initialize it to 1 (since any single element is an increasing subsequence).
    • Set another variable (e.g., currLen) to count the length of the current increasing subsequence. Also start at 1.
  2. Iterate Through the Array:
    • Start from the second element (index 1) and compare it with the previous element.
    • If the current element is greater than the previous one, increment currLen by 1.
    • If not, reset currLen to 1 (since the streak is broken).
  3. Update Maximum:
    • After each comparison, update maxLen if currLen is greater than maxLen.
  4. Edge Cases:
    • If the array is empty, return 0 immediately.
  5. Return Result:
    • After the loop, return maxLen.

This approach only requires a single pass through the array and uses constant extra space.

Example Walkthrough

Let's consider the input nums = [1, 3, 5, 4, 7].

  1. Start: maxLen = 1, currLen = 1
  2. Compare 3 > 1: Yes → currLen = 2, maxLen = 2
  3. Compare 5 > 3: Yes → currLen = 3, maxLen = 3
  4. Compare 4 > 5: No → currLen = 1
  5. Compare 7 > 4: Yes → currLen = 2 (but maxLen remains 3)

The longest continuous increasing subsequence is [1, 3, 5], with a length of 3. The function returns 3.

Time and Space Complexity

  • Brute-force Approach:
    • Would involve checking every possible subarray, leading to O(n2) time complexity.
    • Space complexity is O(1) if we only keep counters, but O(n) if we store subarrays.
  • Optimized Approach (Our Solution):
    • Time Complexity: O(n), because we only scan the array once.
    • Space Complexity: O(1), since we only use a few variables regardless of input size.

Summary

The key to solving the Longest Continuous Increasing Subsequence problem is recognizing that we can process the array in a single pass, tracking only the current streak and the maximum found so far. This approach is both time and space efficient, making it suitable for large inputs. By resetting the current streak whenever the increasing property breaks, we ensure that only continuous sequences are considered, leading to a simple and elegant solution.