← All problems

3105. Longest Strictly Increasing or Strictly Decreasing Subarray

EasyOpen on LeetCodeProblem statement

Problem Statement

3105. Longest Strictly Increasing or Strictly Decreasing Subarray

Easy


You are given an array of integers nums. Return the length of the longest subarray of nums which is either strictly increasing or strictly decreasing.

 

Example 1:

Input: nums = [1,4,3,3,2]

Output: 2

Explanation:

The strictly increasing subarrays of nums are [1], [2], [3], [3], [4], and [1,4].

The strictly decreasing subarrays of nums are [1], [2], [3], [3], [4], [3,2], and [4,3].

Hence, we return 2.

Example 2:

Input: nums = [3,3,3,3]

Output: 1

Explanation:

The strictly increasing subarrays of nums are [3], [3], [3], and [3].

The strictly decreasing subarrays of nums are [3], [3], [3], and [3].

Hence, we return 1.

Example 3:

Input: nums = [3,2,1]

Output: 3

Explanation:

The strictly increasing subarrays of nums are [3], [2], and [1].

The strictly decreasing subarrays of nums are [3], [2], [1], [3,2], [2,1], and [3,2,1].

Hence, we return 3.

 

Constraints:

Java

Source file
class Solution {
    public int longestMonotonicSubarray(int[] nums) {
        int maxMonotonicLen = 1;
        int incStreak = 1;
        int decStreak = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] > nums[i - 1]) {
                incStreak++;
                decStreak = 1;
            } else if (nums[i] < nums[i - 1]) {
                decStreak++;
                incStreak = 1;
            } else
                incStreak = decStreak = 1;
            maxMonotonicLen = Math.max(maxMonotonicLen, Math.max(incStreak, decStreak));
        }
        return maxMonotonicLen;
    }
}