Track the current increasing run and reset it whenever the next value is not greater than its predecessor.
Complexity
O(n) time and O(1) extra space.
Find the length of the longest strictly increasing contiguous segment.
Track the current increasing run and reset it whenever the next value is not greater than its predecessor.
O(n) time and O(1) extra space.
package main
func findLengthOfLCIS(nums []int) int {
best, run := 0, 0
for index, value := range nums {
if index == 0 || value > nums[index-1] {
run++
} else {
run = 1
}
if run > best {
best = run
}
}
return best
}
class Solution:
def findLengthOfLCIS(self, nums: list[int]) -> int:
best = run = 0
for index, value in enumerate(nums):
run = run + 1 if index == 0 or value > nums[index - 1] else 1
best = max(best, run)
return best
function findLengthOfLCIS(nums: number[]): number {
let best = 0;
let run = 0;
for (let index = 0; index < nums.length; index += 1) {
run = index === 0 || nums[index] > nums[index - 1] ? run + 1 : 1;
best = Math.max(best, run);
}
return best;
}