Skip to content

Instantly share code, notes, and snippets.

@joneschris
Last active December 31, 2015 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joneschris/8049901 to your computer and use it in GitHub Desktop.
Save joneschris/8049901 to your computer and use it in GitHub Desktop.
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
print("{}{}".format("X" * currentHeight, "w" * water))
totalWater = totalWater + water
print("Total Water: {}".format(totalWater))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment