962. Maximum Width Ramp

Maximum Width Ramp (LeetCode 962) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Stack Patterns pattern, in the Monotonic Stack subpattern.

How to solve Maximum Width Ramp

Maximum Width Ramp is a Monotonic Stack problem, part of Stack Patterns. Master monotonic stack for next greater element, daily temperatures, and stock span problems.

Work towards the solution in these steps:

  1. Consider using a data structure that helps you efficiently track indices where the array values are in a certain order, like a stack.
  2. Try building a monotonic stack that stores indices of nums in increasing order based on their values to identify potential starting points for ramps.
  3. Once you have the stack, iterate through the array from the end to the beginning to find the farthest index j where nums[j] is greater than or equal to nums[i] from the stack.

The reference solution runs in O(n) time and O(n) space.

The Maximum Width Ramp solution and editorial walks through the full approach with reference code.

Problem statement

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.

Given an integer array nums, return the maximum width of a ramp in nums. If there is no ramp in nums, return 0.

Example 1:

Input: nums = [6,0,8,2,1,5] Output: 4 Explanation: The maximum width ramp is achieved at (i, j) = (1, 5): nums[1] = 0 and nums[5] = 5.

Example 2:

Input: nums = [9,8,1,0,1,9,4,0,4,1] Output: 7 Explanation: The maximum width ramp is achieved at (i, j) = (2, 9): nums[2] = 1 and nums[9] = 1.

Constraints:

- 2 <= nums.length <= 5 104

- 0 <= nums[i] <= 5 104

Topics and companies

Topics: Array, Two Pointers, Stack, Monotonic Stack.

Reported in interviews at Facebook.

How to practise Maximum Width Ramp on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(n) space.

Open Maximum Width Ramp in the code editor, or read the Maximum Width Ramp editorial for a worked solution with its approach and complexity analysis.

Where Maximum Width Ramp sits in the DSA pattern sheet

Problems related to Maximum Width Ramp

Other problems that use the same Monotonic Stack and Stack Patterns techniques:

Maximum Width Ramp FAQ

What is the LeetCode number of Maximum Width Ramp?

Maximum Width Ramp is LeetCode problem 962, rated Medium.

What pattern is used to solve Maximum Width Ramp?

Maximum Width Ramp is a Monotonic Stack problem. It belongs to the Stack Patterns pattern, in the Monotonic Stack subpattern.

What is the time complexity of Maximum Width Ramp?

The reference solution runs in O(n) time and O(n) space.

Which companies ask Maximum Width Ramp?

Maximum Width Ramp has been reported in interviews at Facebook.

Preparing this workspace.