mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #315 from fusunx/master
0135.分发糖果.md Javascript 37515f2
This commit is contained in:
@ -176,7 +176,30 @@ class Solution:
|
|||||||
|
|
||||||
Go:
|
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
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user