Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 788ff22

Browse files
committed
Minor refactoring of dpMaximumSubarray.
1 parent 814fa77 commit 788ff22

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

‎src/algorithms/sets/maximum-subarray/dpMaximumSubarray.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export default function dpMaximumSubarray(inputArray) {
1818
let currentSum = 0;
1919

2020
// 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.
2223
let maxStartIndex = 0;
23-
let maxEndIndex = inputArray.length;
24-
24+
let maxEndIndex = inputArray.length - 1;
2525
let currentStartIndex = 0;
2626

2727
inputArray.forEach((currentNumber, currentIndex) => {
@@ -31,7 +31,7 @@ export default function dpMaximumSubarray(inputArray) {
3131
if (maxSum < currentSum) {
3232
maxSum = currentSum;
3333
maxStartIndex = currentStartIndex;
34-
maxEndIndex = currentIndex+1;
34+
maxEndIndex = currentIndex;
3535
}
3636

3737
// Reset currentSum and currentStartIndex if currentSum drops below 0.
@@ -41,5 +41,5 @@ export default function dpMaximumSubarray(inputArray) {
4141
}
4242
});
4343

44-
return inputArray.slice(maxStartIndex, maxEndIndex);
44+
return inputArray.slice(maxStartIndex, maxEndIndex+1);
4545
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /