Skip to content

Instantly share code, notes, and snippets.

@CheezItMan
Last active May 5, 2019 23:29
Show Gist options
  • Save CheezItMan/6eea11befe5da6b36608a2f0f7c18a83 to your computer and use it in GitHub Desktop.
Save CheezItMan/6eea11befe5da6b36608a2f0f7c18a83 to your computer and use it in GitHub Desktop.
Find the max consecutive integers in an array
# @param {Integer} num1, {Integer} num2
# @return {Integer}
def max(num1, num2)
return num1 > num2 ? num1: num2
end
# @param {Integer[]} nums
# @return {Integer}
def max_sub_array(nums)
return 0 if nums == nil
current_max = max_so_far = nums.shift
nums.each do | num|
current_max = max(num, current_max + num)
max_so_far = max(max_so_far, current_max)
end
return max_so_far
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment