Longest Continuous Increasing Subsequence

easy array

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].

Inputnums = [1, 3, 5, 4, 7]
Output3
The longest run is [1, 3, 5] of length 3 (the next run [4, 7] is only length 2).

def 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;
}
Time: O(n) Space: O(1)