-
-
Save dmitry/287bd6cad9abc70436f4 to your computer and use it in GitHub Desktop.
ruby solutions for 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
{ | |
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