Trapping Rain Water

hard
Unit 24 · Heaps0/2 to milestone

Given bar heights, how much rain water is trapped between them? Water above a bar is limited by the shorter of the tallest bars to its left and right.

Implement trapped_water(heights) in O(n)O(n) time and O(1)O(1) extra space, using two pointers that move inward from both ends and track the running maximum on each side.

#two-pointers#arrays2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Trapping Rain Water · TechGradify