Last active
December 31, 2015 21:58
-
-
Save joneschris/8049901 to your computer and use it in GitHub Desktop.
Twitter Interview Solution in Python (http://qandwhat.apps.runkite.com/i-failed-a-twitter-interview/ and https://gist.github.com/mkozakov/59af0fd5bddbed1a0399)
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
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