Guest User

Untitled

a guest
Feb 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. require "enumerator"
  2.  
  3. # tree is represented as: [[a,b,c,d],[e,f,g],[h,i],[j]]
  4. # (bottom up)
  5. #
  6. def max(tree)
  7. bottom = tree.shift
  8. pairs = bottom.enum_for(:each_cons, 2).to_a
  9. update = pairs.enum_for(:each_with_index).map do |pair,i|
  10. tree.first[i] + pair.max
  11. end
  12.  
  13. tree.first.replace(update)
  14. tree.length > 1 ? max(tree) : tree.flatten.first
  15. end
Add Comment
Please, Sign In to add comment