Skip to content

Instantly share code, notes, and snippets.

heights = [2, 5, 1, 2, 3, 4, 7, 7, 6]
totalWater = 0
for index in range(0, len(heights)):
currentHeight = heights[index]
maxLeftHeight = max(heights[:index]) if index > 0 else 0
maxRightHeight = max(heights[index:])
smallestMaxHeight = min(maxLeftHeight, maxRightHeight)
water = smallestMaxHeight - currentHeight if smallestMaxHeight - currentHeight > 0 else 0