☰ All problems

Trapping Rain Water

A 2D platform game builds its terrain from columns of blocks. Column i is height[i] blocks tall, every column is exactly one block wide, and the columns stand side by side with no gaps.

When it rains, water settles in the dips between taller columns, while any water that reaches the left or right edge of the level drains away. Return how many blocks of water the terrain holds once the rain stops.

Example 1
Input: height = [4,1,3,0,2]
Output: 4

Explanation: Two blocks of water sit above column 1 and two above column 3. The water over column 3 is capped by the last column, which is only 2 tall.

Example 2
Input: height = [5,2,2,5]
Output: 6

Explanation: The flat basin fills up to height 5: 3 + 3 = 6 blocks.

Example 3
Input: height = [3,2,1]
Output: 0

Explanation: The terrain only slopes down, so all the rain drains off the right edge.

Constraints

  • The terrain has between 1 and 20,000 columns.
  • Each column is between 0 and 10^5 blocks tall.
💡 Hint 1

The water above bar i is min(tallest bar to its left, tallest bar to its right) - height[i] (counting bar i itself on both sides).

💡 Hint 2

Precomputing the left and right maxima in two arrays gives an O(n) time, O(n) space solution.

💡 Hint 3

For O(1) space, use two pointers. Whichever side has the smaller maximum so far is the bottleneck, so the water over that pointer is already known.

/**
 * @param {number[]} height
 * @return {number}
 */
function trap(height) {

}
Ctrl/⌘ + ' run · Ctrl/⌘ + Enter submit
esc