-
Notifications
You must be signed in to change notification settings - Fork 4
/
candy.js
48 lines (42 loc) · 886 Bytes
/
candy.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
var DEBUG = process.env.DEBUG;
/**
* @param {number[]} ratings
* @return {number}
*/
var candy = function(ratings) {
if (ratings.length === 0) return 0;
var v = [1];
var count = 1;
for (var i = 1; i < ratings.length; i++) {
if (ratings[i-1] < ratings[i]) {
count++;
v.push(count);
} else {
count = 1;
v.push(count);
}
}
for (var i = ratings.length - 2; i >= 0; i--) {
if (ratings[i+1] < ratings[i]) {
v[i] = Math.max(v[i], v[i+1]+1);
}
}
return v.reduce(function (prev, item) {
return prev + item;
}, 0);
};
function test(f) {
[
[[1]],
[[2]],
[[2,2]],
[[3,2,1]],
[[3,2,1,1,2,3]],
[[3,3,2,1,1,2,3,3]],
[require('./candy-input1.js')],
[require('./candy-input2.js')]
].forEach(function (input) {
console.log(f.apply(undefined, input));
});
}
if (DEBUG) test(candy);