The problem. Given n non-negative integers height describing an elevation map where every bar has width 1, compute how much water it can trap after raining.
Input: height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] Output: 6
Input: height = [4, 2, 0, 3, 2, 5] Output: 9Think about one column. Water above it rises until it would spill over the lower of the two "walls" around it: the tallest bar anywhere to its left and the tallest bar anywhere to its right.
Members only · free account
The rest of this lesson, with 5 more sections, 2 drawings, 2 dry-run simulators and code in JavaScript, Python, Java and C++, opens with a free account.
Was this helpful?
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson