Skip to content

Instantly share code, notes, and snippets.

@imouaddine
Last active August 29, 2015 14:08
Show Gist options
  • Save imouaddine/f61376d93c1df5cb197b to your computer and use it in GitHub Desktop.
Save imouaddine/f61376d93c1df5cb197b to your computer and use it in GitHub Desktop.
Largest Sum Contiguous Subarray
def max_subarray(a)
max = - Float::INFINITY
max_at = []
sum = 0
sum_at = []
a.each_with_index do |e, i|
sum += e
sum_at << e
sum_at = [e] if sum < e
sum = [sum, e].max
if sum > max
max = sum
max_at = sum_at.clone
end
end
return max, max_at
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment