Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created October 15, 2008 04:49
Show Gist options
  • Select an option

  • Save practicingruby/16862 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/16862 to your computer and use it in GitHub Desktop.
require "enumerator"
# tree is represented as: [[a,b,c,d],[e,f,g],[h,i],[j]]
# (bottom up)
#
def max(tree)
bottom = tree.shift
pairs = bottom.enum_for(:each_cons, 2).to_a
update = pairs.enum_for(:each_with_index).map do |pair,i|
tree.first[i] + pair.max
end
tree.first.replace(update)
tree.length > 1 ? max(tree) : tree.flatten.first
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment