Maximum Width Ramp - Complete Solution Guide
Maximum Width Ramp is LeetCode problem 962, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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,
Detailed Explanation
The problem asks us to find the maximum width of a 'ramp' in an array of integers. A ramp is defined as a pair of indices (i, j) such that i < j and nums[i] <= nums[j]. The width of the ramp is j - i. The goal is to find the pair (i, j) that satisfies these conditions and maximizes the width j - i. If no such ramp exists, we should return 0. The input is an array of integers, and the output is the maximum width of the ramp.
Solution Approach
The solution utilizes a monotonic stack to efficiently find the maximum width ramp. First, it constructs a stack of indices corresponding to decreasing elements in the input array. Then, it iterates through the array from the end, attempting to form a ramp with the elements in the stack. As it iterates from the end, it checks if the current element is greater than or equal to the element at the top of the stack. If it is, a ramp is found, and the width is calculated. The algorithm keeps track of the maximum width found so far and returns it at the end.
Step-by-Step Algorithm
- Step 1: Initialize an empty stack `s` to store indices and a variable `max_width` to 0.
- Step 2: Iterate through the input array `nums` from left to right (index `i`).
- Step 3: For each element `nums[i]`, check if the stack is empty or if `nums[i]` is smaller than the element at the index on top of the stack (`nums[s[-1]]`).
- Step 4: If the condition in step 3 is true, push the current index `i` onto the stack. This builds a decreasing stack of indices, ensuring the stack only holds the indices of potential starting points for the ramp.
- Step 5: Iterate through the input array `nums` from right to left (index `j`).
- Step 6: While the stack is not empty and the element `nums[j]` is greater than or equal to the element at the index on top of the stack (`nums[s[-1]]`), pop the index from the stack.
- Step 7: Calculate the width of the ramp as `j - popped_index`. Update `max_width` to be the maximum of `max_width` and the calculated width.
- Step 8: After iterating through the array from right to left, return `max_width`.
Key Insights
- Insight 1: Constructing a monotonic stack of indices is crucial. We need to maintain a stack of indices that correspond to decreasing elements in the input array. This allows us to efficiently find potential starting points (i) for the ramp.
- Insight 2: Iterating from the end of the array allows us to maximize the width of the ramp. By scanning from the end, we can try to match each element with the smallest element to its left within the built stack to get the maximum possible width.
- Insight 3: The stack only stores potential 'left' ends of the ramp. Indices are added to the stack if the element at that index is smaller than any element already represented in the stack. This ensures that we only consider the smallest possible 'left' element for a ramp, which is optimal for maximizing width.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Two Pointers, Stack, Monotonic Stack.
Companies
Asked at: Zepto.