This commit is contained in:
Ilya Kantor 2016-07-17 16:53:32 +03:00
parent 776e83e1f1
commit 480e69b843
87 changed files with 1020 additions and 233 deletions

View file

@ -0,0 +1,11 @@
function getMaxSubSum(arr) {
let maxSum = 0;
let partialSum = 0;
for (let item of arr) {
partialSum += item;
maxSum = Math.max(maxSum, partialSum);
if (partialSum < 0) partialSum = 0;
}
return maxSum;
}

View file

@ -0,0 +1,33 @@
describe("getMaxSubSum", function() {
it("maximal subsum of [1, 2, 3] equals 6", function() {
assert.equal(getMaxSubSum([1, 2, 3]), 6);
});
it("maximal subsum of [-1, 2, 3, -9] equals 5", function() {
assert.equal(getMaxSubSum([-1, 2, 3, -9]), 5);
});
it("maximal subsum of [-1, 2, 3, -9, 11] equals 11", function() {
assert.equal(getMaxSubSum([-1, 2, 3, -9, 11]), 11);
});
it("maximal subsum of [-2, -1, 1, 2] equals 3", function() {
assert.equal(getMaxSubSum([-2, -1, 1, 2]), 3);
});
it("maximal subsum of [100, -9, 2, -3, 5] equals 100", function() {
assert.equal(getMaxSubSum([100, -9, 2, -3, 5]), 100);
});
it("maximal subsum of [] equals 0", function() {
assert.equal(getMaxSubSum([]), 0);
});
it("maximal subsum of [-1] equals 0", function() {
assert.equal(getMaxSubSum([-1]), 0);
});
it("maximal subsum of [-1, -2] equals 0", function() {
assert.equal(getMaxSubSum([-1, -2]), 0);
});
});

View file

@ -0,0 +1,95 @@
# The slow solution
We can calculate all possible subsums.
The simplest way is to take every element and calculate sums of all subarrays starting from it.
For instance, for `[-1, 2, 3, -9, 11]`:
```js no-beautify
// Starting from -1:
-1
-1 + 2
-1 + 2 + 3
-1 + 2 + 3 + (-9)
-1 + 2 + 3 + (-9) + 11
// Starting from 2:
2
2 + 3
2 + 3 + (-9)
2 + 3 + (-9) + 11
// Starting from 3:
3
3 + (-9)
3 + (-9) + 11
// Starting from -9
-9
-9 + 11
// Starting from -11
-11
```
The code is actually a nested loop: the external loop over array elements, and the internal counts subsums starting with the current element.
```js run
function getMaxSubSum(arr) {
let maxSum = 0; // if we take no elements, zero will be returned
for (let i = 0; i < arr.length; i++) {
let sumFixedStart = 0;
for (let j = i; j < arr.length; j++) {
sumFixedStart += arr[j];
maxSum = Math.max(maxSum, sumFixedStart);
}
}
return maxSum;
}
alert( getMaxSubSum([-1, 2, 3, -9]) ); // 5
alert( getMaxSubSum([-1, 2, 3, -9, 11]) ); // 11
alert( getMaxSubSum([-2, -1, 1, 2]) ); // 3
alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([100, -9, 2, -3, 5]) ); // 100
```
The solution has a time complexety of [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation). In other words, if we increase the array size 2 times, the algorithm will work 4 times longer.
For big arrays (1000, 10000 or more items) such algorithms can lead to a seroius sluggishness.
# Fast solution
Let's walk the array and keep the current partial sum of elements in the variable `s`. If `s` becomes negative at some point, then assign `s=0`. The maximum of all such `s` will be the answer.
If the description is too vague, please see the code, it's short enough:
```js run
function getMaxSubSum(arr) {
let maxSum = 0;
let partialSum = 0;
for (let item of arr; i++) { // for each item of arr
partialSum += item; // add it to partialSum
maxSum = Math.max(maxSum, partialSum); // remember the maximum
if (partialSum < 0) partialSum = 0; // zero if negative
}
return maxSum;
}
alert( getMaxSubSum([-1, 2, 3, -9]) ); // 5
alert( getMaxSubSum([-1, 2, 3, -9, 11]) ); // 11
alert( getMaxSubSum([-2, -1, 1, 2]) ); // 3
alert( getMaxSubSum([100, -9, 2, -3, 5]) ); // 100
alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([-1, -2, -3]) ); // 0
```
The algorithm requires exactly 1 array pass, so the time complexity is O(n).
You can find more detail information about the algorithm here: [Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem). If it's still not obvious why that works, then please trace the algorithm on the examples above, see how it works, that's better than any words.

View file

@ -0,0 +1,30 @@
importance: 2
---
# A maximal subarray
The input is an array of numbers, e.g. `arr = [1, -2, 3, 4, -9, 6]`.
The task is: find the contiguous subarray of `arr` with the maximal sum of items.
Write the function `getMaxSubSum(arr)` that will find return that sum.
For instance:
```js
getMaxSubSum([-1, *!*2, 3*/!*, -9]) = 5 (the sum of highlighted items)
getMaxSubSum([*!*2, -1, 2, 3*/!*, -9]) = 6
getMaxSubSum([-1, 2, 3, -9, *!*11*/!*]) = 11
getMaxSubSum([-2, -1, *!*1, 2*/!*]) = 3
getMaxSubSum([*!*100*/!*, -9, 2, -3, 5]) = 100
getMaxSubSum([*!*1, 2, 3*/!*]) = 6 (take all)
```
If all items are negative, it means that we take none (the subarray is empty), so the sum is zero:
```js
getMaxSubSum([-1, -2, -3]) = 0
```
Please try to think of a fast solution: [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation) or even O(n) if you can.