Skip to content

Instantly share code, notes, and snippets.

@deliro
Created November 7, 2013 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deliro/7350277 to your computer and use it in GitHub Desktop.
Save deliro/7350277 to your computer and use it in GitHub Desktop.
def calculate_volume(relief):
left_max = 0
right_max = 0
left = 0
right = len(relief) - 1
volume = 0
while left < right:
if relief[left] > left_max:
left_max = relief[left]
if relief[right] > right_max:
right_max = relief[right]
if left_max >= right_max:
volume += right_max - relief[right]
right -= 1
else :
volume += left_max - relief[left]
left += 1
return volume
a = [10, 9, 8, 7, 6, 5, 4]
print(calculate_volume(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment