programming-examples/ruby/Algorithms/max_subsequence.rb

12 lines
193 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
def maximum_subsequence(arr)
max_so_far = 0
max_total = 0
arr.each do |el|
max_so_far = [max_so_far + el, 0].max
max_total = [max_total, max_so_far].max
end
max_total
end