Skip to content

Instantly share code, notes, and snippets.

@ddollar
Created July 29, 2009 20:50
Show Gist options
  • Select an option

  • Save ddollar/158387 to your computer and use it in GitHub Desktop.

Select an option

Save ddollar/158387 to your computer and use it in GitHub Desktop.
def flatten_with_sums(triangle)
row_index = triangle.length - 2
while row_index >= 0
row = triangle[row_index]
next_row = triangle[row_index + 1]
row.each_with_index do |value, index|
row[index] = [value + next_row[index], value + next_row[index + 1]].max
end
row_index -= 1
end
triangle
end
puts flatten_with_sums(T).first.first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment