15% off all plans & roadmaps Use code
See plansLeetCode 962 · Solution & editorial
A ramp in an integer array nums is a pair (i, j) for which i < j and nums[i] <= nums[j]. The width of such a ramp is j - i.
Maximum Width Ramp is a Stack Patterns problem, in the Monotonic Stack subpattern. The pattern covers parentheses matching, monotonic stack, histogram, and expression evaluation
Master monotonic stack for next greater element, daily temperatures, and stock span problems.
The reference solution runs in O(n) time and O(n) space.
Maximum Width Ramp has been reported in interviews at Facebook.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Maximum Width Ramp is a Stack Patterns problem, in the Monotonic Stack subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.