Created
March 7, 2020 09:45
-
-
Save deque-blog/15bbc418a49bb67e316f3bc1eb9b4a45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def trap(heights: List[int]) -> int: | |
left_h = 0 | |
total_volume = 0 | |
for i in range(1, len(heights)-1): | |
h = heights[i] | |
left_h = max(heights[i-1], left_h) | |
right_h = max(heights[i+1:], default=0) | |
volume = max(0, min(left_h, right_h) - h) | |
total_volume += volume | |
return total_volume |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment