Hone logo
Hone
Problems

Trapping Rain Water

You are given a digital terrain represented by an array of non-negative integers, where each integer represents the height of a bar at a specific position. Imagine it rains heavily on this terrain. Your task is to calculate the total amount of rainwater that can be trapped between these bars. This is a common problem in computational geometry and algorithm design, illustrating how to efficiently process spatial data.

Problem Description

Given an array of non-negative integers height, representing the heights of bars at each position, calculate the total amount of rainwater that can be trapped.

What needs to be achieved: Calculate the total volume of water that can be held within the depressions formed by the bars.

Key requirements:

  • The terrain is formed by adjacent bars of equal width (assumed to be 1 unit).
  • Water can only be trapped if there are higher bars to its left and right, forming a "container".
  • The amount of water trapped at any given position is determined by the minimum height of the tallest bar to its left and the tallest bar to its right, minus the current bar's height. If this difference is negative, no water is trapped at that position.

Expected behavior: The function should return a single non-negative integer representing the total trapped rainwater.

Important edge cases to consider:

  • Empty input array.
  • Array with only one or two elements (no water can be trapped).
  • Arrays with all bars of the same height.
  • Arrays with bars of strictly increasing or decreasing heights.
  • Arrays with a single peak or valley.

Examples

Example 1:

Input: height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
Output: 6
Explanation:
The image above represents the elevation map where the blue areas represent water.
At index 1 (height 1): Water trapped = min(1, 3) - 1 = 0 (incorrect, it's min(left_max, right_max) - height)
Correction for explanation:
At index 2 (height 0): Left max = 1, Right max = 3. Water = min(1, 3) - 0 = 1
At index 4 (height 1): Left max = 2, Right max = 3. Water = min(2, 3) - 1 = 1
At index 5 (height 0): Left max = 2, Right max = 3. Water = min(2, 3) - 0 = 2
At index 6 (height 1): Left max = 2, Right max = 3. Water = min(2, 3) - 1 = 1
At index 9 (height 1): Left max = 3, Right max = 2. Water = min(3, 2) - 1 = 1
Total water = 1 + 1 + 2 + 1 + 1 = 6

Example 2:

Input: height = [4, 2, 0, 3, 2, 5]
Output: 9
Explanation:
At index 1 (height 2): Left max = 4, Right max = 5. Water = min(4, 5) - 2 = 2
At index 2 (height 0): Left max = 4, Right max = 5. Water = min(4, 5) - 0 = 4
At index 3 (height 3): Left max = 4, Right max = 5. Water = min(4, 5) - 3 = 1
At index 4 (height 2): Left max = 4, Right max = 5. Water = min(4, 5) - 2 = 2
Total water = 2 + 4 + 1 + 2 = 9

Example 3: (Edge Case)

Input: height = [5, 4, 3, 2, 1]
Output: 0
Explanation: The heights are strictly decreasing, so no water can be trapped.

Constraints

  • 0 <= height.length <= 2 * 10^4
  • 0 <= height[i] <= 10^5
  • The input height will be an array of non-negative integers.
  • Your solution should aim for an efficient time complexity.

Notes

Consider how the amount of water at each position depends on its immediate surroundings. You might find it helpful to pre-compute or efficiently determine the maximum height to the left and right of each bar. Think about how to avoid redundant calculations.

Loading editor...
plaintext