All problems
1095EasyArray

The Longest Run That Only Climbs or Only Falls

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3105Longest Strictly Increasing or Strictly Decreasing Subarray

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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