Created
March 29, 2018 19:54
-
-
Save Ch4s3/9881c4240e82bd93d9023a636b9f774a to your computer and use it in GitHub Desktop.
ruby maximum hourglass sum
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
def sum_hour_glass(c, r, two_d_array) | |
top = two_d_array[r][c..c+2].inject(:+) | |
center = two_d_array[r +1][c+1] | |
bottom = two_d_array[r+2][c..c+2].inject(:+) | |
top + center + bottom | |
end | |
def array2D(arr) | |
max = -1.0/0.0 | |
arr.each_with_index do |row, r_i| | |
break if r_i > 3 | |
row.each_with_index do |el, c_i| | |
next if c_i > 3 | |
sum = sum_hour_glass(c_i, r_i, arr) | |
max = sum if sum > max | |
end | |
end | |
max | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment