|
6 | 6 | * @return {Number[]}
|
7 | 7 | */
|
8 | 8 | export default function dpMaximumSubarray(inputArray) {
|
9 | | - // Check if all elements of inputArray are negative ones and return the highest |
10 | | - // one in this case. |
11 | | - let allNegative = true; |
12 | | - let highestElementValue = null; |
13 | | - for (let i = 0; i < inputArray.length; i += 1) { |
14 | | - if (inputArray[i] >= 0) { |
15 | | - allNegative = false; |
16 | | - } |
17 | | - |
18 | | - if (highestElementValue === null || highestElementValue < inputArray[i]) { |
19 | | - highestElementValue = inputArray[i]; |
20 | | - } |
21 | | - } |
22 | | - |
23 | | - if (allNegative && highestElementValue !== null) { |
24 | | - return [highestElementValue]; |
25 | | - } |
26 | | - |
27 | | - // Let's assume that there is at list one positive integer exists in array. |
28 | | - // And thus the maximum sum will for sure be grater then 0. Thus we're able |
29 | | - // to always reset max sum to zero. |
30 | | - let maxSum = 0; |
31 | | - |
32 | | - // This array will keep a combination that gave the highest sum. |
33 | | - let maxSubArray = []; |
34 | | - |
35 | | - // Current sum and subarray that will memoize all previous computations. |
| 9 | + // We iterate through the inputArray once, using a greedy approach |
| 10 | + // to keep track of the maximum sum we've seen so far and the current sum |
| 11 | + // |
| 12 | + // currentSum gets reset to 0 everytime it drops below 0 |
| 13 | + // |
| 14 | + // maxSum is set to -Infinity so that if all numbers |
| 15 | + // are negative, the highest negative number will constitute |
| 16 | + // the maximum subarray |
| 17 | + let maxSum = -Infinity; |
36 | 18 | let currentSum = 0;
|
37 | | - let currentSubArray = []; |
38 | 19 |
|
39 | | - for (let i = 0; i < inputArray.length; i += 1) { |
40 | | - // Let's add current element value to the current sum. |
41 | | - currentSum += inputArray[i]; |
| 20 | + // We need to keep track of the starting and ending indices that |
| 21 | + // contributed to our maxSum so that we can return the actual subarray |
| 22 | + let maxStartIndex = 0; |
| 23 | + let maxEndIndex = inputArray.length; |
| 24 | + let currentStartIndex = 0; |
| 25 | + |
| 26 | + inputArray.forEach((num, currentIndex) => { |
| 27 | + currentSum += num; |
| 28 | + |
| 29 | + // Update maxSum and the corresponding indices |
| 30 | + // if we have found a new max |
| 31 | + if (maxSum < currentSum) { |
| 32 | + maxSum = currentSum; |
| 33 | + maxStartIndex = currentStartIndex; |
| 34 | + maxEndIndex = currentIndex + 1; |
| 35 | + } |
42 | 36 |
|
| 37 | + // Reset currentSum and currentStartIndex |
| 38 | + // if currentSum drops below 0 |
43 | 39 | if (currentSum < 0) {
|
44 | | - // If the sum went below zero then reset it and don't add current element to max subarray. |
45 | 40 | currentSum = 0;
|
46 | | - // Reset current subarray. |
47 | | - currentSubArray = []; |
48 | | - } else { |
49 | | - // If current sum stays positive then add current element to current sub array. |
50 | | - currentSubArray.push(inputArray[i]); |
51 | | - |
52 | | - if (currentSum > maxSum) { |
53 | | - // If current sum became greater then max registered sum then update |
54 | | - // max sum and max subarray. |
55 | | - maxSum = currentSum; |
56 | | - maxSubArray = currentSubArray.slice(); |
57 | | - } |
| 41 | + currentStartIndex = currentIndex + 1; |
58 | 42 | }
|
59 | | - } |
| 43 | + }); |
60 | 44 |
|
61 | | - return maxSubArray; |
| 45 | + return inputArray.slice(maxStartIndex,maxEndIndex); |
62 | 46 | }
|
0 commit comments