Skip to content

Instantly share code, notes, and snippets.

@sghall
Created January 23, 2017 08:53
Show Gist options
  • Save sghall/1745a23a9aed9781a115660434788332 to your computer and use it in GitHub Desktop.
Save sghall/1745a23a9aed9781a115660434788332 to your computer and use it in GitHub Desktop.
Max Sub-Array JavaScript
https://en.wikipedia.org/wiki/Maximum_subarray_problem
function maxSubArray(arr) {
const len = arr.length;
let cur = arr[0];
let max = arr[0];
for (let i = 1; i < len; i++) {
cur = Math.max(arr[i], cur + arr[i]);
max = Math.max(max, cur);
}
return max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment