Movatterモバイル変換


[0]ホーム

URL:


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

Commit788ff22

Browse files
committed
Minor refactoring of dpMaximumSubarray.
1 parent814fa77 commit788ff22

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
letcurrentSum=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
letmaxStartIndex=0;
23-
letmaxEndIndex=inputArray.length;
24-
24+
letmaxEndIndex=inputArray.length-1;
2525
letcurrentStartIndex=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-
returninputArray.slice(maxStartIndex,maxEndIndex);
44+
returninputArray.slice(maxStartIndex,maxEndIndex+1);
4545
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp