DDSA
Advertisement

3105. Longest Strictly Increasing or Strictly Decreasing Subarray

Time: O(n)
Space: O(1)

Approach

Track current run lengths for both increasing and decreasing; update max.

3105.cs
C#
// Approach: Track current run lengths for both increasing and decreasing; update max.
// Time: O(n) Space: O(1)

public class Solution
{
    public int LongestMonotonicSubarray(int[] nums)
    {
        int ans = 1;
        int increasing = 1;
        int decreasing = 1;

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

        return ans;
    }
}
Advertisement
Was this solution helpful?