Trapping Rain Water

To solve this coding challenge of calculating the amount of trapped rainwater in a given elevation map, we can utilize the concept of finding the maximum heights to the left and right of each element in the array. By comparing these maximum heights, we can determine the water level at each position. Below is a # Explanation section that thoroughly elaborates on the solution along with detailed pseudocode with comments.

Explanation:

  1. Understanding the Problem : The input is an array where each element’s value represents the height of bars at each index. Our task is to calculate how much water can be trapped between these bars after it rains. The water trapped at a position depends on the heights of bars to its left and right.
  2. Calculate Maximum Heights From Left and Right :
    • For each position in the array, you need to know the highest bar on its left and the highest bar on its right.
    • Left_max
      : Create an array where each position will store the maximum height observed from the left side up to that position.
    • Right_max
      : Similarly, create another array where each position will store the maximum height observed from the right side up to that position.
  3. Water Level Calculation :
    • For each position, the water level is determined by the minimum of the maximum heights from the left and right.
    • If this water level is higher than the height of the bar at that position, the difference (water level minus bar height) is the amount of water that can be stored at that position.
  4. Summing Up the Water Trapped :
    • Sum up the water trapped at each position to get the total amount of trapped water.
    Detailed Steps with Pseudocode:
  5. Early Exit :
    • If the array is empty, immediately return 0, because no water can be trapped in an empty array.
                                                
    # Check if the height array is empty
    if height array is empty:
    return 0
    
                                            
  6. Initialize Arrays :
    • Create two arrays,
      left_max
      and
      right_max
      , each of the same length as the input array.
    • Initialize the first element of
      left_max
      to the first element of the input array.
    • Initialize the last element of
      right_max
      to the last element of the input array.
                                                
    # Initialize left_max and right_max arrays
    set left_max to an array of zeros of the same length as the height array
    set right_max to an array of zeros of the same length as the height array
    
    # Initialize the first element of left_max and the last element of right_max
    left_max[0] = height[0]
    right_max[-1] = height[-1]
    
                                            
  7. Fill Left_max Array :
    • Iterate through the height array from the second element to the last.
    • For each position, set
      left_max[i]
      to the maximum of
      left_max[i-1]
      and
      height[i]
      .
                                                
    # Fill the left_max array
    for each element in height array starting from the second element:
    left_max[i] = maximum of left_max[i-1] and height[i]
    
                                            
  8. Fill Right_max Array :
    • Iterate through the height array from the second last element to the first.
    • For each position, set
      right_max[i]
      to the maximum of
      right_max[i+1]
      and
      height[i]
      .
                                                
    # Fill the right_max array
    for each element in height array starting from the second last element to the first:
    right_max[i] = maximum of right_max[i+1] and height[i]
    
                                            
  9. Calculate Total Trapped Water :
    • Initialize
      total_water
      to 0.
    • Iterate through the height array excluding the first and last elements (since water cannot be trapped at the edges).
    • For each position, calculate the water level as the minimum of
      left_max[i]
      and
      right_max[i]
      .
    • If this water level is greater than the height at that position, add the difference to
      total_water
      .
                                                
    # Initialize total trapped water to 0
    total_water = 0
    
    # Calculate water trapped at each position
    for each element in height array excluding the first and last elements:
    water_level = minimum of left_max[i] and right_max[i]
    if water_level > height[i]:
    total_water += water_level - height[i]
    
                                            
  10. Return Result :
    • Return the
      total_water
      which is the sum of water trapped at all positions.
                                            
# Return the total trapped water
return total_water

                                        
This algorithm ensures that by making two passes through the height array to fill the
left_max
and
right_max
arrays, and one more pass to calculate the trapped water, we run efficiently even for large arrays. With this detailed explanation and pseudocode, you can clearly understand the logic and easily implement it in any programming language.