42. Trapping Rain Water

42. Trapping Rain Water

Difficulty: Hard

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.

Example 1:

Trapping Rain Water

  • Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
  • Output: 6
  • Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.

Example 2:

  • Input: height = [4,2,0,3,2,5]
  • Output: 9

Constraints:

  • n == height.length
  • <code>1 <= n <= 2 * 104
  • <code>0 <= height[i] <= 105

Solution:

To solve this problem, we can follow these steps:

  1. Initialize two pointers: left starting at the beginning and right at the end of the elevation array.
  2. Track maximum heights: Use left_max and right_max to track the maximum heights encountered from the left and right sides, respectively.
  3. Move pointers towards each other: At each step, move the pointer with the smaller height inward and update the trapped water amount based on the minimum of left_max and right_max.

Let’s implement this solution in PHP: 42. Trapping Rain Water

<?php
// Example usage:
$height = [0,1,0,2,1,0,1,3,2,1,2,1];
echo trap($height); // Output: 6

$height = [4,2,0,3,2,5];
echo trap($height); // Output: 9

?>

Explanation:

  1. Initialize pointers and maximum heights: Start with left at the beginning and right at the end of the height array. Also, initialize left_max and right_max to 0.
  2. Traverse the array with two pointers:
    • Compare height[left] and height[right].
    • If height[left] is less than or equal to height[right]:
      • If height[left] is greater than or equal to left_max, update left_max.
      • Otherwise, calculate the water trapped at left and add to water.
      • Move left pointer one step to the right.
    • If height[right] is less than height[left]:
      • If height[right] is greater than or equal to right_max, update right_max.
      • Otherwise, calculate the water trapped at right and add to water.
      • Move right pointer one step to the left.
  3. Return the total trapped water: Once the two pointers meet, the water variable holds the total amount of trapped rainwater.

This approach efficiently computes the amount of trapped rainwater by leveraging the two-pointer technique and avoids the need for additional space, making it optimal for large input sizes.

Contact Links

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

If you want more helpful content like this, feel free to follow me:

 

Read More

If you need any support regarding your website

If you like our blog, Please support us to improve

Buy Me a Coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

RECENT POST
Leetcode Solutions

633. Sum of Square Numbers

Sum of Square Numbers Difficulty: Medium Topics: Math, Two Pointers, Binary Search Given a non-negative integer c, decide whether there’re

Leetcode Solutions

624. Maximum Distance in Arrays

Maximum Distance in Arrays Difficulty: Medium Topics: Array, Greedy You are given m arrays, where each array is sorted in