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.
Free account
Sign up to read the rest of this lesson: 5 more sections, 3 drawings, 2 dry-run simulators and code in JavaScript, Python, Java and C++.
Still to come
Was this helpful?