962. Maximum Width Ramp LeetCode Solution

In this guide, you will get 962. Maximum Width Ramp LeetCode Solution with the best time and space complexity. The solution to Maximum Width Ramp problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Width Ramp solution in C++
  4. Maximum Width Ramp solution in Java
  5. Maximum Width Ramp solution in Python
  6. Additional Resources
962. Maximum Width Ramp LeetCode Solution image

Problem Statement of Maximum Width Ramp

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

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

962. Maximum Width Ramp LeetCode Solution in C++

class Solution {
 public:
  int maxWidthRamp(vector<int>& nums) {
    int ans = 0;
    stack<int> stack;

    for (int i = 0; i < nums.size(); ++i)
      if (stack.empty() || nums[i] < nums[stack.top()])
        stack.push(i);

    for (int i = nums.size() - 1; i > ans; --i)
      while (!stack.empty() && nums[i] >= nums[stack.top()])
        ans = max(ans, i - stack.top()), stack.pop();

    return ans;
  }
};
/* code provided by PROGIEZ */

962. Maximum Width Ramp LeetCode Solution in Java

class Solution {
  public int maxWidthRamp(int[] nums) {
    int ans = 0;
    Deque<Integer> stack = new ArrayDeque<>();

    for (int i = 0; i < nums.length; ++i)
      if (stack.isEmpty() || nums[i] < nums[stack.peek()])
        stack.push(i);

    for (int i = nums.length - 1; i > ans; --i)
      while (!stack.isEmpty() && nums[i] >= nums[stack.peek()])
        ans = Math.max(ans, i - stack.pop());

    return ans;
  }
}
// code provided by PROGIEZ

962. Maximum Width Ramp LeetCode Solution in Python

class Solution:
  def maxWidthRamp(self, nums: list[int]) -> int:
    ans = 0
    stack = []

    for i, num in enumerate(nums):
      if stack == [] or num <= nums[stack[-1]]:
        stack.append(i)

    for i, num in reversed(list(enumerate(nums))):
      while stack and num >= nums[stack[-1]]:
        ans = max(ans, i - stack.pop())

    return ans
# code by PROGIEZ

Additional Resources

See also  769. Max Chunks To Make Sorted LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.