Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1c828f2 commit 1fe106dCopy full SHA for 1fe106d
src/searching/maximum-subarray.js
@@ -20,11 +20,10 @@
20
* @return {Number} Maximum sum of the elements of a subarray.
21
*/
22
function maxSubarray(array) {
23
- var currentMax = 0;
24
- var max = 0;
25
-
26
- for (var i = 0; i < array.length; i += 1) {
27
- currentMax = Math.max(0, currentMax + array[i]);
+ var currentMax = array[0];
+ var max = array[0];
+ for (var i = 1; i < array.length; i += 1) {
+ currentMax = Math.max(array[i], currentMax + array[i]);
28
max = Math.max(max, currentMax);
29
}
30
return max;
0 commit comments