Created
January 23, 2017 08:53
-
-
Save sghall/1745a23a9aed9781a115660434788332 to your computer and use it in GitHub Desktop.
Max Sub-Array JavaScript
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
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