Show problems as
Classic problem #3105EasyArray

Longest Strictly Increasing or Strictly Decreasing Subarray

Real-world version: The Longest Run That Only Climbs or Only Falls
Tracked in this browser only
Read Longest Strictly Increasing or Strictly Decreasing Subarray on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A strip of readings reads readings.

Return the length of the longest run of neighbouring readings that either climbs strictly all the way or falls strictly all the way. A run of a single reading counts as both.

Examples

Example 1

Input
readings = [1, 2, 5, 4]
Output
3

The first three readings climb strictly, which is a run of three. The fall at the end reaches only two.

Example 2

Input
readings = [8, 8]
Output
1

The two readings are equal, so neither climbs nor falls, leaving runs of one.

Example 3

Input
readings = [5, 4, 3, 4, 5, 6]
Output
4

The strip falls for three readings and then climbs for four, so the climbing run is the longer.

Constraints

  • 1 <= readings.length <= 50
  • 1 <= readings[i] <= 50

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def longest_monotonic_subarray(readings: list[int]) -> int:
Java
public int longestMonotonicSubarray(int[] readings)