Skip to content

Instantly share code, notes, and snippets.

@dmitry
Last active December 26, 2015 23:39
Show Gist options
  • Save dmitry/287bd6cad9abc70436f4 to your computer and use it in GitHub Desktop.
Save dmitry/287bd6cad9abc70436f4 to your computer and use it in GitHub Desktop.
{
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
},
easy: -> land {
v = 0
land.each_with_index do |h, i|
v += [[land[0..i].max, land[i..-1].max].min - h, 0].max
end
v
}
}.each do |name, func|
print name.to_s
{
[1,2,3,4,5,4,5,3] => 1,
[2,1,2,3,4,2] => 1,
[5,1,5] => 4,
[2,7,2,7,4,7,1,7,3,7] => 5+3+6+4,
[5,1,0,1] => 1,
[2,5,1,2,3,4,7,7,6,3,5] => 12
}.each do |land, volume|
print func.call(land) == volume ? '.' : 'F'
end
print "\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment