Loading...
Loading...
Loading...
Walls of different heights stand in a row, each one unit wide. After rain, water collects between walls. Print the total number of water units trapped. Use two pointers and O(1) extra space.
Example 1
Input
0 1 0 2 1 0 1 3 2 1 2 1
Output
6
Six units are trapped.
Example 2
Input
4 2 0 3 2 5
Output
9
Try the problem first. Open one hint at a time.
Water above a wall is limited by the shorter of the tallest wall on its left and the tallest on its right.
Keep the tallest wall seen from each side. Move the pointer on the side with the smaller tallest wall, because that side decides its water.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor