674. Longest Continuous Increasing Subsequence¶
Edited by Charles
Easy · Python · Dynamic Programming
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Given an integer array nums, find the length of the Longest Continuous Increasing Subsequence (LCIS).
Unlike Longest Increasing Subsequence (LIS), the elements in LCIS must be contiguous.
Original study notes follow, including intermediate snippets and earlier attempts.
Idea¶
Given an integer array nums, find the length of the Longest Continuous Increasing Subsequence (LCIS).
Unlike Longest Increasing Subsequence (LIS), the elements in LCIS must be contiguous.
For example:
The longest continuous increasing subsequence is:
Its length is 3.
Dynamic Programming¶
State Definition¶
Let
denote:
The length of the longest continuous increasing subsequence ending at
nums[i].
Since the subsequence must be continuous, the previous element can only be nums[i-1].
For example:
Then
Initialization¶
Each element itself forms a continuous increasing subsequence of length 1.
Therefore,
That is,
State Transition¶
Since the subsequence must be continuous, we only need to compare the current element with its immediate previous element.
If
then nums[i] can extend the previous continuous increasing subsequence.
Update:
Otherwise,
since a new subsequence starts at nums[i].
Transition equation:
The key observation is that continuity removes the need to examine all previous elements.
Why Do We Need a Global Maximum?¶
The longest continuous increasing subsequence does not necessarily end at the last element.
For example,
Then
The answer is
instead of
Therefore, we maintain a variable res to record the maximum value throughout the traversal.
Example¶
Initialization:
i = 1, nums[1] = 3¶
Extend the previous subsequence:
i = 2, nums[2] = 5¶
Continue extending:
i = 3, nums[3] = 4¶
The increasing sequence is broken.
Start a new subsequence:
i = 4, nums[4] = 7¶
Extend the new subsequence:
Final answer:
Complexity Analysis¶
Time Complexity
Each element is visited exactly once.
Space Complexity
A DP array of length n is used.
Note: Since
dp[i]only depends ondp[i-1], the space complexity can be optimized to O(1) by keeping only the current length.
Summary¶
State Definition
Initialization
State Transition
Traversal Order
Traverse the array from left to right, comparing each element with its immediate predecessor.
Final Answer
or maintain a running maximum during traversal.
Common Mistakes¶
- This problem asks for a continuous increasing subsequence, so elements must be adjacent.
- Unlike LeetCode 300 (Longest Increasing Subsequence), there is no need to iterate through all previous elements. Only compare
nums[i]withnums[i - 1]. - Do not return
dp[-1], since the longest continuous increasing subsequence does not necessarily end at the last element. - The sequence must be strictly increasing, so the condition should be
instead of
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
Python solution¶
class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
l = len(nums)
dp = [1] * l
res = 1
for i in range(1, l):
if nums[i] > nums[i - 1]:
dp[i] = dp[i - 1] + 1
res = max(res, dp[i])
return res