Skip to content

Instantly share code, notes, and snippets.

@collinwu
Created November 2, 2015 05:39
Show Gist options
  • Save collinwu/a7f096aaa8f074ad9dcf to your computer and use it in GitHub Desktop.
Save collinwu/a7f096aaa8f074ad9dcf to your computer and use it in GitHub Desktop.
max sequence
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