Longest Strictly Increasing or Strictly Decreasing Subarray (LeetCode 3105): Solution and Explanation
Given an array of integers, return the length of the longest contiguous subarray that is strictly increasing or strictly decreasing. Equal neighbours do not count as either.
Intuition
Checking every subarray works for 50 elements, but it hides a simpler structure. Any strictly monotonic subarray ending at index i is a suffix of either the longest increasing run ending at i or the longest decreasing run ending at i.
Those two runs can be updated from index i-1 by looking at a single pair. If nums[i] > nums[i-1], the climb continues and the fall is over. If nums[i] < nums[i-1], the fall continues and the climb is over. If they are equal, neither strict run survives.
So keep two counters, inc and dec, that both describe runs ending at the current index. After each update the answer so far is the largest of best, inc and dec.
Approach: Two running lengths
- Set
best = inc = dec = 1, since one element is a valid run of both kinds. - For each i from 1, compare
nums[i]withnums[i-1]. - On a rise,
inc += 1anddec = 1. On a drop,dec += 1andinc = 1. On a tie, set both to 1. - Update
best = max(best, inc, dec)and returnbestafter the loop.
Step-by-step example
nums = [1,4,3,3,2]
- Start with best = inc = dec = 1.
- 1 < 4: inc = 2, dec = 1, best = 2.
- 4 > 3: dec = 2, inc = 1, best stays 2.
- 3 = 3: both runs reset to 1.
- 3 > 2: dec = 2, inc = 1, best stays 2. Return 2.
Code: Python, Java, C++
1class Solution:
2 def longestMonotonicSubarray(self, nums: List[int]) -> int:
3 best = inc = dec = 1
4 for i in range(1, len(nums)):
5 if nums[i] > nums[i - 1]:
6 inc, dec = inc + 1, 1
7 elif nums[i] < nums[i - 1]:
8 inc, dec = 1, dec + 1
9 else:
10 inc = dec = 1
11 best = max(best, inc, dec)
12 return best1class Solution {
2 public int longestMonotonicSubarray(int[] nums) {
3 int best = 1, inc = 1, dec = 1;
4 for (int i = 1; i < nums.length; i++) {
5 if (nums[i] > nums[i - 1]) {
6 inc++;
7 dec = 1;
8 } else if (nums[i] < nums[i - 1]) {
9 dec++;
10 inc = 1;
11 } else {
12 inc = 1;
13 dec = 1;
14 }
15 best = Math.max(best, Math.max(inc, dec));
16 }
17 return best;
18 }
19}1class Solution {
2public:
3 int longestMonotonicSubarray(vector<int>& nums) {
4 int best = 1, inc = 1, dec = 1;
5 for (int i = 1; i < nums.size(); i++) {
6 if (nums[i] > nums[i - 1]) {
7 inc++;
8 dec = 1;
9 } else if (nums[i] < nums[i - 1]) {
10 dec++;
11 inc = 1;
12 } else {
13 inc = 1;
14 dec = 1;
15 }
16 best = max(best, max(inc, dec));
17 }
18 return best;
19 }
20};Time and space complexity
O(n)
Each element is compared with its left neighbour once.
O(1)
Only three counters are kept.
Other approaches
Brute force over all subarrays (time O(n²), space O(1))
For every start index, extend to the right while the subarray stays strictly increasing, then do the same for strictly decreasing, and keep the longest length.
Two separate passes (time O(n), space O(1))
Find the longest strictly increasing run in one pass and the longest strictly decreasing run in another, then return the larger. Same result with twice the scanning.
Common mistakes
- Treating equal neighbours as part of a run. The problem asks for strict order, so a tie must reset both counters.
- Resetting a counter to 0 instead of 1. The current element always forms a run of length 1 on its own.
- Forgetting the single-element case, where the answer is 1 and the loop body never runs.
- Only updating
bestwhen a run breaks, which misses a run that lasts until the end of the array.
Frequently asked questions
Why can both counters be tracked together?
Each comparison of a neighbour pair is a rise, a drop or a tie. A rise can only extend the increasing run, a drop only the decreasing run, and a tie neither, so one pass updates both in O(1).
What is the answer when every element is equal?
It is 1. Every pair is a tie, so both runs keep resetting to a single element.
Is this the same as the longest increasing subsequence?
No. A subarray must be contiguous, so a single break ends the run. The subsequence problem allows skipping elements and needs dynamic programming or binary search.