Longest Continuous Increasing Subsequence — LeetCode 674 Python Solution

EasyArray
Problem
#674
Reading time
2 min

The problem

Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (i.e. subarray).

Example

Input
nums = [1,3,5,4,7]
Output
3
Explanation
The longest continuous increasing subsequence is [1,3,5] with length 3.

Python solution

Python
class Solution:
    def findLengthOfLCIS(self, nums: List[int]) -> int:
        ans = cnt = 1
        for i, x in enumerate(nums[1:]):
            if nums[i] < x:
                cnt += 1
                ans = max(ans, cnt)
            else:
                cnt = 1
        return ans

Complexity

MeasureComplexity
TimeO(n), where n is the length of the array nums
SpaceO(1) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 674. Longest Continuous Increasing Subsequence?
LeetCode 674. Longest Continuous Increasing Subsequence is rated Easy on LeetCode.
What is the time complexity of LeetCode 674. Longest Continuous Increasing Subsequence?
The Python solution on this page runs in O(n), where n is the length of the array nums.
What is the space complexity of LeetCode 674. Longest Continuous Increasing Subsequence?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 674. Longest Continuous Increasing Subsequence cover?
LeetCode 674. Longest Continuous Increasing Subsequence is tagged Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview