Last active
May 5, 2019 23:29
-
-
Save CheezItMan/6eea11befe5da6b36608a2f0f7c18a83 to your computer and use it in GitHub Desktop.
Find the max consecutive integers in an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @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