-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
maximum-subarray-sum.js
45 lines (39 loc) · 1.15 KB
/
maximum-subarray-sum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var maxSequence = function (arr) {
// initialize maxSum to 0
let maxSum = arr[0];
// iterate from 0 to end of array - i
for (let i = 0; i < arr.length; i++) {
// iterate from current index to end of array - j
for (let j = i; j < arr.length; j++) {
// place to store sum of current subarray
let currentSum = 0;
// iterate from i to j
for (let k = i; k < j; k++) {
// sum all numbers
currentSum += arr[k];
}
// if currentSum is greater than maxSum
if (currentSum > maxSum) {
// set MaxSum
maxSum = currentSum;
}
}
}
return maxSum;
}
// single iteration
var maxSequence = function (arr) {
// place keep track of currentSum // initialize to 0
let currentSum = 0;
// place to keep track of maxSum // initialize to 0
let maxSum = 0;
// iterate over array
for (let i = 0; i < arr.length; i++) {
const currentNumber = arr[i];
// set currentSum to max of currentSum + currentNumber and 0
currentSum = Math.max(currentSum + currentNumber, 0);
// set maxSum to max of currentSum and maxSum
maxSum = Math.max(currentSum, maxSum);
}
return maxSum;
}