Skip to content

Commit 49788ab

Browse files
Merge pull request #315 from fusunx/master
0135.分发糖果.md Javascript 37515f2
2 parents 34db204 + 10a2089 commit 49788ab

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

problems/0135.分发糖果.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,30 @@ class Solution:
176176

177177
Go:
178178

179+
Javascript:
180+
```Javascript
181+
var candy = function(ratings) {
182+
let candys = new Array(ratings.length).fill(1)
183+
184+
for(let i = 1; i < ratings.length; i++) {
185+
if(ratings[i] > ratings[i - 1]) {
186+
candys[i] = candys[i - 1] + 1
187+
}
188+
}
179189

190+
for(let i = ratings.length - 2; i >= 0; i--) {
191+
if(ratings[i] > ratings[i + 1]) {
192+
candys[i] = Math.max(candys[i], candys[i + 1] + 1)
193+
}
194+
}
195+
196+
let count = candys.reduce((a, b) => {
197+
return a + b
198+
})
199+
200+
return count
201+
};
202+
```
180203

181204

182205
-----------------------

0 commit comments

Comments
 (0)