250x250
반응형
Notice
Recent Posts
Recent Comments
Link
N
(Leet Code JS)Candy 본문
728x90
반응형
https://leetcode.com/problems/candy/
var candy = function(ratings) {
const { length } = ratings;
const fromLeft = Array.from({length}, () => 1);
const fromRight = Array.from({length}, () => 1);
let answer = 0;
for(let i = 1; i < length; i++){
const current = ratings[i];
const before = ratings[i - 1];
if(current > before){
fromLeft[i] = fromLeft[i - 1] + 1;
}
}
for(let i = length - 2; i >= 0; i--){
const current = ratings[i];
const next = ratings[i + 1];
if(current > next){
fromRight[i] = fromRight[i + 1] + 1;
}
}
for(let i = 0; i < length; i++){
answer += Math.max(fromLeft[i], fromRight[i]);
}
return answer;
};
728x90
반응형
'Leet Code 알고리즘' 카테고리의 다른 글
(Leet Code JS)Diagonal Traverse (0) | 2022.07.30 |
---|---|
(Leet Code JS)Game of Life (0) | 2022.07.30 |
(Leet Code JS)Wiggle Subsequence (0) | 2022.07.09 |
(Leet Code JS)Maximum Units on a Truck (0) | 2022.07.09 |
(Leet Code JS)Furthest Building You Can Reach (0) | 2022.07.03 |