Classic problem #3105EasyArray
Longest Strictly Increasing or Strictly Decreasing Subarray
Real-world version: The Longest Run That Only Climbs or Only FallsTracked 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)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's