Longest Continuous Increasing Subsequence (LeetCode 674): Solution and Explanation
Walk the array once, remember where the current strictly increasing run began, restart it at every drop or plateau, and keep the longest run length.
Intuition
Picture the array as a staircase. A continuous increasing run is a stretch where every step goes up.
Whether a run continues depends only on one comparison: is nums[i] bigger than nums[i - 1]? If not, the staircase has a drop or a flat step, and no run can cross that point.
So the array splits into independent climbs. Track the start of the current climb, and its length i - start + 1 at every step; the answer is the largest such length.
Approach: Single pass with a run start
- Set best = 1 and start = 0, since one element is a run of length 1.
- For each i from 1 to n - 1, if nums[i] <= nums[i - 1], set start = i.
- Update best = max(best, i - start + 1).
- Return best.
Step-by-step example
nums = [1,3,5,4,7]
- Start: best = 1, the run begins at index 0.
- i = 1: 3 > 1, the run [1,3] has length 2. best = 2.
- i = 2: 5 > 3, the run [1,3,5] has length 3. best = 3.
- i = 3: 4 <= 5, the climb breaks. A new run starts at index 3 with length 1.
- i = 4: 7 > 4, the run [4,7] has length 2. best stays 3.
- The answer is 3.
Code: Python, Java, C++
1class Solution:
2 def findLengthOfLCIS(self, nums: List[int]) -> int:
3 best = 1
4 start = 0
5 for i in range(1, len(nums)):
6 if nums[i] <= nums[i - 1]:
7 start = i
8 best = max(best, i - start + 1)
9 return best1class Solution {
2 public int findLengthOfLCIS(int[] nums) {
3 int best = 1;
4 int start = 0;
5 for (int i = 1; i < nums.length; i++) {
6 if (nums[i] <= nums[i - 1]) start = i;
7 best = Math.max(best, i - start + 1);
8 }
9 return best;
10 }
11}1class Solution {
2public:
3 int findLengthOfLCIS(vector<int>& nums) {
4 int best = 1;
5 int start = 0;
6 for (int i = 1; i < (int)nums.size(); i++) {
7 if (nums[i] <= nums[i - 1]) start = i;
8 best = max(best, i - start + 1);
9 }
10 return best;
11 }
12};Time and space complexity
O(n)
Each index is compared with its left neighbour exactly once.
O(1)
Only the run start and the best length are stored.
Other approaches
Run length counter (time O(n), space O(1))
Keep a counter that grows by 1 when nums[i] > nums[i - 1] and drops back to 1 otherwise. It is the same idea without storing the start index.
Check every subarray (time O(n^2), space O(1))
For each start index, extend while values keep increasing. Simple, but it rescans the same climbs many times.
Common mistakes
- Treating equal neighbours as increasing. The run must be strictly increasing, so [2,2] breaks the run.
- Confusing this with Longest Increasing Subsequence. Here the elements must be adjacent, so a simple scan is enough and no dynamic programming is needed.
- Starting best at 0. Any non-empty array has a run of length at least 1.
Frequently asked questions
Why is a single pass enough?
A run can only break between two neighbours, and once it breaks no later run can include the earlier elements. So each element belongs to exactly one maximal climb, and one pass sees every climb.
How is this different from Longest Increasing Subsequence?
A subsequence may skip elements, which needs DP or patience sorting. This problem asks for a contiguous subarray, so only neighbouring comparisons matter.
What about negative numbers?
They change nothing. The algorithm only compares neighbours, so the sign of the values does not matter.