Created
September 22, 2019 15:24
-
-
Save DmytroVasin/7773fee91360b754b86f69f4fb8af06f 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
## EASY: | |
``` | |
def trap(height) | |
drops = 0 | |
height.each_with_index do |current_height, i| | |
max_occupancy = [height[0..i].max, height[i..-1].max].min | |
drops += max_occupancy - current_height | |
end | |
drops | |
end | |
``` | |
## HARD: | |
``` | |
{ | |
original: -> land { | |
left_max = 0 | |
right_max = 0 | |
left = 0 | |
right = land.length - 1 | |
volume = 0 | |
while left < right | |
if land[left] > left_max | |
left_max = land[left] | |
end | |
if land[right] > right_max | |
right_max = land[right] | |
end | |
if left_max >= right_max | |
volume += right_max - land[right] | |
right -= 1 | |
else | |
volume += left_max - land[left] | |
left += 1 | |
end | |
end | |
volume | |
}, | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment