-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximum-sum-subarray-problem.js
59 lines (37 loc) · 1.74 KB
/
maximum-sum-subarray-problem.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* WIP: JavaScript Implementation as I understand it now, probabaly wrong.
Next steps:
* understand question better:
- http://www.geeksforgeeks.org/largest-sum-contiguous-subarray/
- https://stackoverflow.com/questions/32367032/kadanes-algorithm-explained
* redo if need to
view code: https://codepad.remoteinterview.io/PHBVDAUEPG
The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:
maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])
// should be 6: [4, -1, 2, 1]
Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead.
Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray.
/* START *//* verstion 1.0.0 */
function maxSequenceV1 (sequence) {
let sum = 0, tempSum = 0;
sequence.forEach(function(item, index, array) {
tempSum = tempSum + item;
if (tempSum < 0) {
tempSum = 0;
}
if (tempSum > sum) {
sum = tempSum;
}
});
return sum;
}
/* END *//* verstion 1.0.0 */
function assertEquals (testValue, expectedResult) {
if (testValue === expectedResult) {
console.log('SUCCESS: ' + testValue + ' equals ' + expectedResult);
} else {
console.log('FAIL: ' + testValue + ' does not equal to ' + expectedResult);
}
}
assertEquals(maxSequenceV1([]), 0);
assertEquals(maxSequenceV1([-1, -2, -3]), 0);
assertEquals(maxSequenceV1([-2, 1, -3, 4, -1, 2, 1, -5, 4]), 6);