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
The problem asks to find the maximum width of a ramp in an integer array nums
. A ramp is defined as a pair of indices (i, j)
where i < j
and nums[i] <= nums[j]
. The width of the ramp is j - i
.
Two main approaches are presented to solve this problem: using a monotonic stack and sorting.
This approach leverages the properties of a monotonic stack to efficiently find the maximum width ramp.
Algorithm:
Initialization: Create an empty stack stk
to store indices.
First Pass (Monotonic Stack Construction): Iterate through the nums
array. If the stack is empty or the current element nums[i]
is smaller than the element at the top of the stack (nums[stk[-1]]
), push the index i
onto the stack. This ensures the stack maintains a strictly decreasing order of elements.
Second Pass (Ramp Width Calculation): Iterate through the nums
array in reverse order. For each element, pop elements from the stack as long as the element at the top of the stack is less than or equal to the current element. For each popped element, calculate the ramp width (i - stk.pop()
) and update the maximum width ans
if necessary.
Return Maximum Width: Return the maximum width ans
.
Time Complexity Analysis:
nums
. While there are nested loops within the second pass, the total number of pops from the stack is at most n, leading to O(n) overall.Space Complexity Analysis:
This approach uses sorting to find the maximum width ramp.
Algorithm:
Pair Indices with Values: Create an array of pairs, where each pair contains the value nums[i]
and its index i
.
Sort the Pairs: Sort the pairs based on their values in ascending order. If values are equal, maintain the original order (to preserve the i < j
condition).
Iterate and Update Maximum Width: Initialize j
to the length of nums
. Iterate through the sorted pairs. For each pair, update the maximum width ans
as max(ans, i - j)
, where i
is the index from the current pair, and then update j
as min(j, i)
.
Return Maximum Width: Return the maximum width ans
.
Time Complexity Analysis:
Space Complexity Analysis:
Approach 1 (Monotonic Stack):
def maxWidthRamp(nums):
stk = []
for i, v in enumerate(nums):
if not stk or nums[stk[-1]] > v:
stk.append(i)
ans = 0
for i in range(len(nums) - 1, -1, -1):
while stk and nums[stk[-1]] <= nums[i]:
ans = max(ans, i - stk.pop())
if not stk:
break
return ans
Approach 2 (Sorting):
def maxWidthRamp(nums):
idx = sorted([(nums[i], i) for i in range(len(nums))])
ans = 0
j = len(nums)
for _, i in idx:
ans = max(ans, i - j)
j = min(j, i)
return ans
The choice between the two approaches depends on the specific requirements. If time complexity is critical and n is very large, the monotonic stack approach (O(n)) is preferable. If the input size is relatively small, the sorting approach (O(n log n)) might be simpler to implement and easier to understand. All other code examples in different languages follow a similar structure and logic based on the selected approach.