Algorithm


Problem Name: 674. Longest Continuous Increasing Subsequence

Given an unsorted array of integers nums, return the length of the longest continuous increasing subsequence (i.e. subarray). The subsequence must be strictly increasing.

A continuous increasing subsequence is defined by two indices l and r (l < r) such that it is [nums[l], nums[l + 1], ..., nums[r - 1], nums[r]] and for each l <= i < r, nums[i] < nums[i + 1].

 

Example 1:

Input: nums = [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5] with length 3.
Even though [1,3,5,7] is an increasing subsequence, it is not continuous as elements 5 and 7 are separated by element
4.

Example 2:

Input: nums = [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2] with length 1. Note that it must be strictly
increasing.

 

Constraints:

  • 1 <= nums.length <= 104
  • -109 <= nums[i] <= 109

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        if(nums.empty()) return 0;
        int i = 0, j = 1, maxlen = 1;
        while(j < nums.size()){
            while(j  <  nums.size(> && nums[j] > nums[j - 1]) j++;
            maxlen = max(maxlen, j - i);
            i = j++;
        }
        return maxlen;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,3,5,4,7]

Output

x
+
cmd
3

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int findLengthOfLCIS(int[] nums) {
    int maxLength = 1;
    int idx = 0;
    while (idx  <  nums.length) {
      int count = 1;
      while (idx < nums.length && idx + 1 < nums.length && nums[idx] < nums[idx + 1]) {
        idx++;
        count++;
      }
      maxLength = Math.max(maxLength, count);
      idx++;
    }
    return maxLength;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,3,5,4,7]

Output

x
+
cmd
3

#3 Code Example with Javascript Programming

Code - Javascript Programming


const findLengthOfLCIS = function(nums) {
  if (nums.length == 1) {
    return 1;
  }
  let ans = 0,
    anchor = 0;
  for (let i = 1; i  <  nums.length; i++) {
    if (nums[i - 1] >= nums[i]) {
      anchor = i;
    }
    ans = Math.max(ans, i - anchor + 1);
  }
  return ans;
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [2,2,2,2,2]

Output

x
+
cmd
1

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def findLengthOfLCIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if nums==[]: return 0
        curr, mx=1, 1
        for i in range(len(nums)-1):
            if nums[i+1]>nums[i]: curr+=1; mx=max(mx,curr)
            else: curr=1
        return mx
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [2,2,2,2,2]

Output

x
+
cmd
1

#5 Code Example with C# Programming

Code - C# Programming


using System;

namespace LeetCode
{
    public class _0674_LongestContinuousIncreasingSubsequence
    {
        public int FindLengthOfLCIS(int[] nums)
        {
            if (nums.Length  < = 1) return nums.Length;

            var max = 1;
            var current = 1;
            for (int i = 1; i  <  nums.Length; i++)
            {
                if (nums[i] > nums[i - 1])
                {
                    current++;
                    max = Math.Max(max, current);
                }
                else
                    current = 1;
            }

            return max;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,3,5,4,7]

Output

x
+
cmd
3
Advertisements

Demonstration


Previous
#673 Leetcode Number of Longest Increasing Subsequence Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#675 Leetcode Cut Off Trees for Golf Event Solution in C, C++, Java, JavaScript, Python, C# Leetcode