-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandy.js
More file actions
40 lines (36 loc) · 935 Bytes
/
Copy pathcandy.js
File metadata and controls
40 lines (36 loc) · 935 Bytes
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
/**
* @param {number[]} ratings
* @return {number}
*/
/**
Input: ratings = [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
*/
// [1, 1, 1]
// [1, 1, 2]
// [2, 1, 2]
var candy = function(ratings) {
let minArr = [];
for(let itr = 0; itr < ratings.length; itr++) {
minArr.push(1);
}
for(let itr =1; itr < ratings.length; itr++) {
if(ratings[itr-1] < ratings[itr]) {
minArr[itr] = minArr[itr-1] +1;
}
}
// console.log(minArr)
for(let itr =ratings.length-2; itr >=0 ; itr--) {
if(ratings[itr] > ratings[itr+1]) {
minArr[itr] = Math.max(minArr[itr+1]+1, minArr[itr] );
}
}
// console.log(minArr)
let sum = 0;
for(let itr = 0; itr < ratings.length; itr++) {
sum += minArr[itr];
}
return sum;
};
//https://leetcode.com/problems/candy/