-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path0135-candy.js
37 lines (33 loc) · 1012 Bytes
/
0135-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
/**
* 135. Candy
* https://leetcode.com/problems/candy/
* Difficulty: Hard
*
* There are n children standing in a line. Each child is assigned a rating value given
* in the integer array ratings.
*
* 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.
*
* Return the minimum number of candies you need to have to distribute the candies to
* the children.
*/
/**
* @param {number[]} ratings
* @return {number}
*/
var candy = function(ratings) {
const data = new Array(ratings.length).fill(1);
for (let i = 1; i < ratings.length; i++) {
if (ratings[i - 1] < ratings[i]) {
data[i] = data[i - 1] + 1;
}
}
for (let i = ratings.length - 1; i > 0; i--) {
if (ratings[i - 1] > ratings[i]) {
data[i - 1] = data[i - 1] > data[i] + 1 ? data[i - 1] : data[i] + 1;
}
}
return data.reduce((sum, n) => sum + n, 0);
};