Created
January 29, 2014 05:38
-
-
Save rahatarmanahmed/8682437 to your computer and use it in GitHub Desktop.
Function to do max subsequence problem
This file contains 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
maxSubSequence = (array) -> | |
max = currentMax = array[0] | |
begin = temp = end = 0 | |
for i in [1..array.length] | |
if currentMax < 0 | |
currentMax = array[i] | |
temp = i | |
else | |
currentMax += array[i] | |
if currentMax >= max | |
max = currentMax | |
begin = temp | |
end = i | |
return [max, begin, end] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment