Created
November 2, 2015 05:39
-
-
Save collinwu/a7f096aaa8f074ad9dcf to your computer and use it in GitHub Desktop.
max sequence
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
var maxSequence = function(arr){ | |
var largestSum = 0; | |
for (var i = 0; i < arr.length; i++) { | |
for (var j = i+1; j <= arr.length; j++) { | |
var currentSeq = arr.slice(i, j); | |
var currentSum = currentSeq.reduce(function(a,b) { | |
return a + b; | |
}, 0); | |
if (currentSum > largestSum) { largestSum = currentSum; } | |
} | |
} | |
return largestSum; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment