Add more test cases for finding max sub-array algorithm.

This commit is contained in:
Oleksii Trekhleb 2018-09-04 11:35:13 +03:00
parent 2a2b5daa7d
commit 814fa773ca
3 changed files with 21 additions and 14 deletions

View File

@ -3,6 +3,10 @@ import bfMaximumSubarray from '../bfMaximumSubarray';
describe('bfMaximumSubarray', () => { describe('bfMaximumSubarray', () => {
it('should find maximum subarray using brute force algorithm', () => { it('should find maximum subarray using brute force algorithm', () => {
expect(bfMaximumSubarray([])).toEqual([]); expect(bfMaximumSubarray([])).toEqual([]);
expect(bfMaximumSubarray([0, 0])).toEqual([0]);
expect(bfMaximumSubarray([0, 0, 1])).toEqual([0, 0, 1]);
expect(bfMaximumSubarray([0, 0, 1, 2])).toEqual([0, 0, 1, 2]);
expect(bfMaximumSubarray([0, 0, -1, 2])).toEqual([2]);
expect(bfMaximumSubarray([-1, -2, -3, -4, -5])).toEqual([-1]); expect(bfMaximumSubarray([-1, -2, -3, -4, -5])).toEqual([-1]);
expect(bfMaximumSubarray([1, 2, 3, 2, 3, 4, 5])).toEqual([1, 2, 3, 2, 3, 4, 5]); expect(bfMaximumSubarray([1, 2, 3, 2, 3, 4, 5])).toEqual([1, 2, 3, 2, 3, 4, 5]);
expect(bfMaximumSubarray([-2, 1, -3, 4, -1, 2, 1, -5, 4])).toEqual([4, -1, 2, 1]); expect(bfMaximumSubarray([-2, 1, -3, 4, -1, 2, 1, -5, 4])).toEqual([4, -1, 2, 1]);

View File

@ -3,6 +3,10 @@ import dpMaximumSubarray from '../dpMaximumSubarray';
describe('dpMaximumSubarray', () => { describe('dpMaximumSubarray', () => {
it('should find maximum subarray using dynamic programming algorithm', () => { it('should find maximum subarray using dynamic programming algorithm', () => {
expect(dpMaximumSubarray([])).toEqual([]); expect(dpMaximumSubarray([])).toEqual([]);
expect(dpMaximumSubarray([0, 0])).toEqual([0]);
expect(dpMaximumSubarray([0, 0, 1])).toEqual([0, 0, 1]);
expect(dpMaximumSubarray([0, 0, 1, 2])).toEqual([0, 0, 1, 2]);
expect(dpMaximumSubarray([0, 0, -1, 2])).toEqual([2]);
expect(dpMaximumSubarray([-1, -2, -3, -4, -5])).toEqual([-1]); expect(dpMaximumSubarray([-1, -2, -3, -4, -5])).toEqual([-1]);
expect(dpMaximumSubarray([1, 2, 3, 2, 3, 4, 5])).toEqual([1, 2, 3, 2, 3, 4, 5]); expect(dpMaximumSubarray([1, 2, 3, 2, 3, 4, 5])).toEqual([1, 2, 3, 2, 3, 4, 5]);
expect(dpMaximumSubarray([-2, 1, -3, 4, -1, 2, 1, -5, 4])).toEqual([4, -1, 2, 1]); expect(dpMaximumSubarray([-2, 1, -3, 4, -1, 2, 1, -5, 4])).toEqual([4, -1, 2, 1]);

View File

@ -6,36 +6,35 @@
* @return {Number[]} * @return {Number[]}
*/ */
export default function dpMaximumSubarray(inputArray) { export default function dpMaximumSubarray(inputArray) {
// We iterate through the inputArray once, using a greedy approach // We iterate through the inputArray once, using a greedy approach to keep track of the maximum
// to keep track of the maximum sum we've seen so far and the current sum // sum we've seen so far and the current sum.
// //
// currentSum gets reset to 0 everytime it drops below 0 // The currentSum variable gets reset to 0 every time it drops below 0.
// //
// maxSum is set to -Infinity so that if all numbers // The maxSum variable is set to -Infinity so that if all numbers are negative, the highest
// are negative, the highest negative number will constitute // negative number will constitute the maximum subarray.
// the maximum subarray
let maxSum = -Infinity; let maxSum = -Infinity;
let currentSum = 0; let currentSum = 0;
// We need to keep track of the starting and ending indices that // We need to keep track of the starting and ending indices that contributed to our maxSum
// contributed to our maxSum so that we can return the actual subarray // so that we can return the actual subarray.
let maxStartIndex = 0; let maxStartIndex = 0;
let maxEndIndex = inputArray.length; let maxEndIndex = inputArray.length;
let currentStartIndex = 0; let currentStartIndex = 0;
inputArray.forEach((num, currentIndex) => { inputArray.forEach((currentNumber, currentIndex) => {
currentSum += num; currentSum += currentNumber;
// Update maxSum and the corresponding indices // Update maxSum and the corresponding indices if we have found a new max.
// if we have found a new max
if (maxSum < currentSum) { if (maxSum < currentSum) {
maxSum = currentSum; maxSum = currentSum;
maxStartIndex = currentStartIndex; maxStartIndex = currentStartIndex;
maxEndIndex = currentIndex + 1; maxEndIndex = currentIndex + 1;
} }
// Reset currentSum and currentStartIndex // Reset currentSum and currentStartIndex if currentSum drops below 0.
// if currentSum drops below 0
if (currentSum < 0) { if (currentSum < 0) {
currentSum = 0; currentSum = 0;
currentStartIndex = currentIndex + 1; currentStartIndex = currentIndex + 1;