File tree 1 file changed +5
-5
lines changed
src/algorithms/sets/maximum-subarray
1 file changed +5
-5
lines changed Original file line number Diff line number Diff line change @@ -18,10 +18,10 @@ export default function dpMaximumSubarray(inputArray) {
18
18
let currentSum = 0 ;
19
19
20
20
// We need to keep track of the starting and ending indices that contributed to our maxSum
21
- // so that we can return the actual subarray.
21
+ // so that we can return the actual subarray. From the beginning let's assume that whole array
22
+ // is contributing to maxSum.
22
23
let maxStartIndex = 0 ;
23
- let maxEndIndex = inputArray . length ;
24
-
24
+ let maxEndIndex = inputArray . length - 1 ;
25
25
let currentStartIndex = 0 ;
26
26
27
27
inputArray . forEach ( ( currentNumber , currentIndex ) => {
@@ -31,7 +31,7 @@ export default function dpMaximumSubarray(inputArray) {
31
31
if ( maxSum < currentSum ) {
32
32
maxSum = currentSum ;
33
33
maxStartIndex = currentStartIndex ;
34
- maxEndIndex = currentIndex + 1 ;
34
+ maxEndIndex = currentIndex ;
35
35
}
36
36
37
37
// Reset currentSum and currentStartIndex if currentSum drops below 0.
@@ -41,5 +41,5 @@ export default function dpMaximumSubarray(inputArray) {
41
41
}
42
42
} ) ;
43
43
44
- return inputArray . slice ( maxStartIndex , maxEndIndex ) ;
44
+ return inputArray . slice ( maxStartIndex , maxEndIndex + 1 ) ;
45
45
}
You can’t perform that action at this time.
0 commit comments