All problems
0947MediumArrayTwo PointersStackMonotonic Stack

Widest Span That Does Not Fall

Tracked in this browser only
Write code

Trains the technique from

LeetCode 962Maximum Width Ramp

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.

Readings are given as nums.

A span is a pair of positions i before j where the later reading is at least the earlier one. Its width is the difference of the positions.

Return the widest span, or 0 when no span exists.

Examples

Example 1

Input
nums = [14, 3, 27, 9, 3, 41]
Output
5

The reading 14 at the front pairs with the 41 at the end, five apart, which is as wide as the list allows.

Example 2

Input
nums = [9, 8, 7, 6, 5, 4]
Output
0

Every reading falls below the one before it, so no pair qualifies.

Example 3

Input
nums = [7, 7, 7, 7, 7]
Output
4

Equal readings count, since the later one only has to be at least the earlier, so the two ends pair up.

Constraints

  • 2 <= nums.length <= 5 * 10^4
  • 0 <= nums[i] <= 5 * 10^4

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 max_width_ramp(nums: list[int]) -> int:
Java
public int maxWidthRamp(int[] nums)
September 7
Apply