-
Notifications
You must be signed in to change notification settings - Fork 130
/
Candy.js
48 lines (39 loc) · 1.17 KB
/
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
/**
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
*/
//TLE
/**
* @param {number[]} ratings
* @return {number}
*/
var candy = function(ratings) {
let len = ratings.length;
let candies = [];
let sum = 1;
candies[0] = 1;
for (let i = 1; i < len; i++) {
if (ratings[i] <= ratings[i - 1]) {
if (candies[i - 1] > 1) {
candies[i] = 1;
sum++;
} else {
candies[i] = 1;
sum++;
let k = i;
while (k > 0 && (ratings[k] < ratings[k - 1]) && (candies[k - 1] <= candies[k])) {
candies[k - 1]++;
sum++;
k--;
}
}
} else {
candies[i] = candies[i - 1] + 1;
sum += candies[i];
}
}
return sum;
};