Last active
February 26, 2016 08:52
-
-
Save amka/1bff9689df3f94a49e1f to your computer and use it in GitHub Desktop.
This file contains 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
class Solution(object): | |
def trap(self, height): | |
""" | |
:type height: List[int] | |
:rtype: int | |
""" | |
water = 0 | |
height_len = len(height) | |
if height_len < 3: | |
return water | |
max_level = max(height) | |
max_level_index = height.index(max_level) | |
prev_level = height[0] | |
for x in range(max_level_index): | |
level = height[x] | |
if level < prev_level and level < max_level: | |
water += (prev_level - level) | |
else: | |
prev_level = level | |
prev_level = height[-1] | |
for x in range(height_len-1, max_level_index, -1): | |
level = height[x] | |
if level < prev_level and level < max_level: | |
water += (prev_level - level) | |
else: | |
prev_level = level | |
return water | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment