Skip to content

Commit 75c6a85

Browse files
committed
🐛 fixed tests and challenge 14
1 parent 0fffc86 commit 75c6a85

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/challenges/14.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
export function maxGifts (houses: number[]): number {
22
const dp = houses.slice(0, 2)
33
for (const house of houses.slice(2)) {
4-
const last = dp[dp.length - 1]
5-
const secondLast = dp[dp.length - 2] + house
6-
dp.push([last, secondLast][+(last < secondLast)])
4+
dp.push(Math.max(...dp.slice(0, -1)) + house)
75
}
8-
return dp[dp.length - 1]
6+
return Math.max(...dp)
97
}

src/tests/14.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,15 @@ describe('Challenge #14', () => {
3333
test('Test #08', () => {
3434
expect(maxGifts([99])).toEqual(99)
3535
})
36+
37+
// These tests below are not in the original challenge, its from the channel
38+
// discussion on Discord.
39+
test('Test #09', () => {
40+
expect(maxGifts([5, 1, 1, 5])).toEqual(10)
41+
})
42+
43+
// https://discord.com/channels/741237973663612969/915910832259477534
44+
test('Test #10', () => {
45+
expect(maxGifts([4, 1, 1, 4, 2, 1])).toEqual(9)
46+
})
3647
})

0 commit comments

Comments
 (0)