mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-27 15:41:16 +08:00
Simplify dpMaximumSubarray (#189)
* Simplify dpMaximumSubarray * change var name from currentMaxSum to currentSum * fix comment with old variable name
This commit is contained in:
parent
6b0bacd993
commit
2a2b5daa7d
@ -6,57 +6,41 @@
|
|||||||
* @return {Number[]}
|
* @return {Number[]}
|
||||||
*/
|
*/
|
||||||
export default function dpMaximumSubarray(inputArray) {
|
export default function dpMaximumSubarray(inputArray) {
|
||||||
// Check if all elements of inputArray are negative ones and return the highest
|
// We iterate through the inputArray once, using a greedy approach
|
||||||
// one in this case.
|
// to keep track of the maximum sum we've seen so far and the current sum
|
||||||
let allNegative = true;
|
//
|
||||||
let highestElementValue = null;
|
// currentSum gets reset to 0 everytime it drops below 0
|
||||||
for (let i = 0; i < inputArray.length; i += 1) {
|
//
|
||||||
if (inputArray[i] >= 0) {
|
// maxSum is set to -Infinity so that if all numbers
|
||||||
allNegative = false;
|
// are negative, the highest negative number will constitute
|
||||||
}
|
// the maximum subarray
|
||||||
|
let maxSum = -Infinity;
|
||||||
if (highestElementValue === null || highestElementValue < inputArray[i]) {
|
|
||||||
highestElementValue = inputArray[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (allNegative && highestElementValue !== null) {
|
|
||||||
return [highestElementValue];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Let's assume that there is at list one positive integer exists in array.
|
|
||||||
// And thus the maximum sum will for sure be grater then 0. Thus we're able
|
|
||||||
// to always reset max sum to zero.
|
|
||||||
let maxSum = 0;
|
|
||||||
|
|
||||||
// This array will keep a combination that gave the highest sum.
|
|
||||||
let maxSubArray = [];
|
|
||||||
|
|
||||||
// Current sum and subarray that will memoize all previous computations.
|
|
||||||
let currentSum = 0;
|
let currentSum = 0;
|
||||||
let currentSubArray = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < inputArray.length; i += 1) {
|
// We need to keep track of the starting and ending indices that
|
||||||
// Let's add current element value to the current sum.
|
// contributed to our maxSum so that we can return the actual subarray
|
||||||
currentSum += inputArray[i];
|
let maxStartIndex = 0;
|
||||||
|
let maxEndIndex = inputArray.length;
|
||||||
|
let currentStartIndex = 0;
|
||||||
|
|
||||||
if (currentSum < 0) {
|
inputArray.forEach((num, currentIndex) => {
|
||||||
// If the sum went below zero then reset it and don't add current element to max subarray.
|
currentSum += num;
|
||||||
currentSum = 0;
|
|
||||||
// Reset current subarray.
|
|
||||||
currentSubArray = [];
|
|
||||||
} else {
|
|
||||||
// If current sum stays positive then add current element to current sub array.
|
|
||||||
currentSubArray.push(inputArray[i]);
|
|
||||||
|
|
||||||
if (currentSum > maxSum) {
|
// Update maxSum and the corresponding indices
|
||||||
// If current sum became greater then max registered sum then update
|
// if we have found a new max
|
||||||
// max sum and max subarray.
|
if (maxSum < currentSum) {
|
||||||
maxSum = currentSum;
|
maxSum = currentSum;
|
||||||
maxSubArray = currentSubArray.slice();
|
maxStartIndex = currentStartIndex;
|
||||||
}
|
maxEndIndex = currentIndex + 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return maxSubArray;
|
// Reset currentSum and currentStartIndex
|
||||||
|
// if currentSum drops below 0
|
||||||
|
if (currentSum < 0) {
|
||||||
|
currentSum = 0;
|
||||||
|
currentStartIndex = currentIndex + 1;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return inputArray.slice(maxStartIndex, maxEndIndex);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user