close
close

962. Maximum Width Driveway – DEV Community

962. Maximum width of ramp

Difficulty: Medium

Topics: Array, Stack, Monotonic Stack

A driveway in an integer array nums is a few (i, j) for what i and nums(i) . The width of such a ramp is j - i.

Given an integer array numsyield the maximum width of a driveway in nums. If there is none driveway in numsyield 0.

Example 1:

  • Input: num = (6,0,8,2,1,5)
  • Exit: 4
  • Explanation: The maximum width slope is reached at (i, j) = (1, 5): nums(1) = 0 and nums(5) = 5.

Example 2:

  • Input: num = (9,8,1,0,1,9,4,0,4,1)
  • Exit: 7
  • Explanation: The maximum width slope is reached at (i, j) = (2, 9): nums(2) = 1 and nums(9) = 1.

Limits:

Solution:

We can use the concept of a monotonous pile. Here is the solution and explanation:

Approximation:

  1. Monotonous decreasing stack: We create a stack that keeps track of the indices of elements in such a way nums(stack(i)) is in descending order. This will allow us to find pairs later (i, j) Where nums(i) .
  2. Traverse from the end: After creating the stack, we traverse the array from the end (j by n-1 Unpleasant 0) to try to find the furthest i for each j Where nums(i) .
  3. Update the maximum width: Whenever nums(i) holds for the current top of the stack, calculate the ramp width j - i and update the maximum width if it is larger.

Let’s implement this solution in PHP: 962. Maximum width of ramp


/**
 * @param Integer() $nums
 * @return Integer
 */
function maxWidthRamp($nums) {
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

// Example 1
$nums = (6, 0, 8, 2, 1, 5);
echo maxWidthRamp($nums);  // Output: 4

// Example 2
$nums = (9, 8, 1, 0, 1, 9, 4, 0, 4, 1);
echo maxWidthRamp($nums);  // Output: 7
?>
Go to full screen mode

Exit full screen mode

Explanation:

  1. Create a decreasing pile:

    • Iterate through the array and add indices to the stack.
    • Add an index only if it matches a value less than or equal to the value of the last index in the stack. This ensures that the values ​​in the stack are in descending order.
  2. Traverse from the end:

    • As we move backward through the array, for each jpop indexes i of the pile as long as nums(i) .
    • Calculate the width j - i and update maxWidth.
  3. Why this works:

    • By maintaining a decreasing stack of indices, we ensure that when we have a j with a larger value it can give us a larger width j - i when i is picked from the pile.
  4. Time complexity:

    • Building the stack takes time O(n) time since each index was pushed once.
    • The passage from the end and the popping indices also last O(n) as each index is displayed at most once.
    • In general, the solution comes in O(n) time, which is efficient for input sizes up to 5 * 10^4.

Exit:

  • For nums = (6, 0, 8, 2, 1, 5)is the output 4corresponding to the driveway (1, 5).
  • For nums = (9, 8, 1, 0, 1, 9, 4, 0, 4, 1)is the output 7corresponding to the driveway (2, 9).

Contact links

If you found this series helpful, please consider reading the warehouse star it on GitHub or share the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, you can follow me: