Trapping Rain Water

You are given n non-negative integers height representing an elevation map where each bar has width 1. Compute how much water it can trap after it rains.

Water above a bar is held in place only if there is a taller (or equal) bar somewhere to its left and somewhere to its right.

Constraints

  • 1 <= height.length <= 2 * 10^4
  • 0 <= height[i] <= 10^5

Examples

Example 1

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

Water sits above indices 2 (1 unit), 4 (1), 5 (2), 6 (1) and 9 (1): 6 units in total.

Example 2

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

Example 3

Input: height = [1]
Output: 0

A single bar can't hold water.

JavaScript you'll use: Numbers, Math and integer tricks Arrays: create, read, update

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

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.