Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dynamic-Programming/MaxNonAdjacentSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function maximumNonAdjacentSum(nums) {
* :return: The maximum non-adjacent sum
*/

if (nums.length < 0) return 0
if (nums.length === 0) return 0

let maxIncluding = nums[0]
let maxExcluding = 0
Expand Down
21 changes: 21 additions & 0 deletions Dynamic-Programming/tests/MaxNonAdjacentSum.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { maximumNonAdjacentSum } from '../MaxNonAdjacentSum'

describe('maximumNonAdjacentSum', () => {
it('should return 0 for an empty array', () => {
expect(maximumNonAdjacentSum([])).toBe(0)
})

it('should return the single element for a one-element array', () => {
expect(maximumNonAdjacentSum([5])).toBe(5)
})

it('should compute the maximum non-adjacent sum', () => {
expect(maximumNonAdjacentSum([1, 2, 3])).toBe(4)
expect(maximumNonAdjacentSum([1, 5, 3, 7, 2, 2, 6])).toBe(18)
expect(maximumNonAdjacentSum([499, 500, -3, -7, -2, -2, -6])).toBe(500)
})

it('should return 0 when all elements are negative', () => {
expect(maximumNonAdjacentSum([-1, -5, -3, -7, -2, -2, -6])).toBe(0)
})
})
Loading