Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment