-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (52 loc) · 1.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// score: 360
function maxGifts(houses) {
let prevMax = 0;
let currMax = 0;
for (let i = 0; i < houses.length; i++) {
console.log(currMax, prevMax);
let newMax = currMax > prevMax + houses[i]
? currMax
: prevMax + houses[i];
prevMax = currMax;
currMax = newMax;
}
return currMax;
}
// score: 290
// function maxGifts(houses) {
// let prevMax = 0;
// let currMax = 0;
// for (const house of houses) {
// let newMax = Math.max(currMax, prevMax + house);
// prevMax = currMax;
// currMax = newMax;
// }
// return currMax;
// }
// score: 290
// function maxGifts(houses) {
// const dp = houses.slice(0, 2);
// for (const house of houses.slice(2)) {
// dp.push(Math.max(dp[dp.length - 1], dp[dp.length - 2] + house))
// }
// return dp[dp.length - 1];
// }
// score: 290
// function maxGifts(houses) {
// let incl = 0;
// let excl = 0;
// let excl_new;
// for (let house of houses) {
// excl_new = [excl, incl][+(incl > excl)];
// incl = excl + house;
// excl = excl_new;
// }
// return [excl, incl][+(incl > excl)];
// }
module.exports = maxGifts;
console.log(maxGifts([2, 4, 2])); // 4 (4)
console.log(maxGifts([5, 1, 1, 5])); // 10 (5 + 5)
console.log(maxGifts([4, 1, 1, 4, 2, 1])); // 9 (4 + 4 + 1)
console.log(maxGifts([1, 3, 1, 3, 100])); // 103 (3 + 100)
console.log(maxGifts([1, 10, 1, 3, 100])); // 110 (10 + 100)
console.log(maxGifts([1, 2, 3, 4, 5])); // 9 (1 + 3 + 5)