From 37515f2202b8a9868a25f8c000f46fe589a093a3 Mon Sep 17 00:00:00 2001 From: fusunx <1102654482@qq.com> Date: Thu, 3 Jun 2021 08:25:39 +0800 Subject: [PATCH] =?UTF-8?q?0135.=E5=88=86=E5=8F=91=E7=B3=96=E6=9E=9C.md=20?= =?UTF-8?q?Javascript?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0135.分发糖果.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md index a3311791..fd791277 100644 --- a/problems/0135.分发糖果.md +++ b/problems/0135.分发糖果.md @@ -176,7 +176,30 @@ class Solution: Go: +Javascript: +```Javascript +var candy = function(ratings) { + let candys = new Array(ratings.length).fill(1) + for(let i = 1; i < ratings.length; i++) { + if(ratings[i] > ratings[i - 1]) { + candys[i] = candys[i - 1] + 1 + } + } + + for(let i = ratings.length - 2; i >= 0; i--) { + if(ratings[i] > ratings[i + 1]) { + candys[i] = Math.max(candys[i], candys[i + 1] + 1) + } + } + + let count = candys.reduce((a, b) => { + return a + b + }) + + return count +}; +``` -----------------------