Longest Continuous Increasing Subsequence
Problem
Given an unsorted array of integers nums, return the length of the longest continuous (strictly) increasing subsequence — i.e. the longest run of indices i..j where nums[k] < nums[k+1] for all k in [i, j-1].
nums = [1, 3, 5, 4, 7]3def find_length_of_lcis(nums):
if not nums:
return 0
best = cur = 1
for i in range(1, len(nums)):
cur = cur + 1 if nums[i] > nums[i-1] else 1
best = max(best, cur)
return best
function findLengthOfLCIS(nums) {
if (nums.length === 0) return 0;
let best = 1, cur = 1;
for (let i = 1; i < nums.length; i++) {
cur = nums[i] > nums[i-1] ? cur + 1 : 1;
if (cur > best) best = cur;
}
return best;
}
class Solution {
public int findLengthOfLCIS(int[] nums) {
if (nums.length == 0) return 0;
int best = 1, cur = 1;
for (int i = 1; i < nums.length; i++) {
cur = nums[i] > nums[i-1] ? cur + 1 : 1;
best = Math.max(best, cur);
}
return best;
}
}
int findLengthOfLCIS(vector<int>& nums) {
if (nums.empty()) return 0;
int best = 1, cur = 1;
for (int i = 1; i < (int)nums.size(); i++) {
cur = nums[i] > nums[i-1] ? cur + 1 : 1;
best = max(best, cur);
}
return best;
}
Explanation
We want the longest run of numbers that keep going up without a break. The neat idea is that we never need to look back — we just keep a running length of the current increasing streak and remember the best streak we have seen.
We keep two counters: cur is the length of the streak ending at the current spot, and best is the longest streak so far. Both start at 1.
At each step, if nums[i] > nums[i-1] the streak continues, so cur grows by one. If not, the streak is broken and cur resets back to 1. After each step we update best.
This works because a continuous increasing run is fully determined by where it last broke, and the reset captures exactly that moment.
Example: [1, 3, 5, 4, 7]. cur climbs 1 to 2 to 3 over 1,3,5, drops to 1 at 4, then back to 2 at 7. The best seen is 3.