Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 814fa77

Browse files
committed
Add more test cases for finding max sub-array algorithm.
1 parent 2a2b5da commit 814fa77

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

‎src/algorithms/sets/maximum-subarray/__test__/bfMaximumSubarray.test.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import bfMaximumSubarray from '../bfMaximumSubarray';
33
describe('bfMaximumSubarray', () => {
44
it('should find maximum subarray using brute force algorithm', () => {
55
expect(bfMaximumSubarray([])).toEqual([]);
6+
expect(bfMaximumSubarray([0, 0])).toEqual([0]);
7+
expect(bfMaximumSubarray([0, 0, 1])).toEqual([0, 0, 1]);
8+
expect(bfMaximumSubarray([0, 0, 1, 2])).toEqual([0, 0, 1, 2]);
9+
expect(bfMaximumSubarray([0, 0, -1, 2])).toEqual([2]);
610
expect(bfMaximumSubarray([-1, -2, -3, -4, -5])).toEqual([-1]);
711
expect(bfMaximumSubarray([1, 2, 3, 2, 3, 4, 5])).toEqual([1, 2, 3, 2, 3, 4, 5]);
812
expect(bfMaximumSubarray([-2, 1, -3, 4, -1, 2, 1, -5, 4])).toEqual([4, -1, 2, 1]);

‎src/algorithms/sets/maximum-subarray/__test__/dpMaximumSubarray.test.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import dpMaximumSubarray from '../dpMaximumSubarray';
33
describe('dpMaximumSubarray', () => {
44
it('should find maximum subarray using dynamic programming algorithm', () => {
55
expect(dpMaximumSubarray([])).toEqual([]);
6+
expect(dpMaximumSubarray([0, 0])).toEqual([0]);
7+
expect(dpMaximumSubarray([0, 0, 1])).toEqual([0, 0, 1]);
8+
expect(dpMaximumSubarray([0, 0, 1, 2])).toEqual([0, 0, 1, 2]);
9+
expect(dpMaximumSubarray([0, 0, -1, 2])).toEqual([2]);
610
expect(dpMaximumSubarray([-1, -2, -3, -4, -5])).toEqual([-1]);
711
expect(dpMaximumSubarray([1, 2, 3, 2, 3, 4, 5])).toEqual([1, 2, 3, 2, 3, 4, 5]);
812
expect(dpMaximumSubarray([-2, 1, -3, 4, -1, 2, 1, -5, 4])).toEqual([4, -1, 2, 1]);

‎src/algorithms/sets/maximum-subarray/dpMaximumSubarray.js‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,35 @@
66
* @return {Number[]}
77
*/
88
export default function dpMaximumSubarray(inputArray) {
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
9+
// We iterate through the inputArray once, using a greedy approach to keep track of the maximum
10+
// sum we've seen so far and the current sum.
1111
//
12-
// currentSum gets reset to 0 everytime it drops below 0
12+
// The currentSum variable gets reset to 0 every time it drops below 0.
1313
//
14-
// maxSum is set to -Infinity so that if all numbers
15-
// are negative, the highest negative number will constitute
16-
// the maximum subarray
14+
// The maxSum variable is set to -Infinity so that if all numbers are negative, the highest
15+
// negativenumber will constitute the maximum subarray.
16+
1717
let maxSum = -Infinity;
1818
let currentSum = 0;
1919

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
20+
// We need to keep track of the starting and ending indices that contributed to our maxSum
21+
// so that we can return the actual subarray.
2222
let maxStartIndex = 0;
2323
let maxEndIndex = inputArray.length;
24+
2425
let currentStartIndex = 0;
2526

26-
inputArray.forEach((num, currentIndex) => {
27-
currentSum += num;
27+
inputArray.forEach((currentNumber, currentIndex) => {
28+
currentSum += currentNumber;
2829

29-
// Update maxSum and the corresponding indices
30-
// if we have found a new max
30+
// Update maxSum and the corresponding indices if we have found a new max.
3131
if (maxSum < currentSum) {
3232
maxSum = currentSum;
3333
maxStartIndex = currentStartIndex;
3434
maxEndIndex = currentIndex + 1;
3535
}
3636

37-
// Reset currentSum and currentStartIndex
38-
// if currentSum drops below 0
37+
// Reset currentSum and currentStartIndex if currentSum drops below 0.
3938
if (currentSum < 0) {
4039
currentSum = 0;
4140
currentStartIndex = currentIndex + 1;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /