找到最大值,后遍历数组,每一个元素加上额外值大于等于最大值,就可以成为拥有最多糖果的孩子
var kidsWithCandies = function(candies, extraCandies) {
let max = Math.max(...candies)
let result = Array.from(candies).fill(false)
for(let i=0; i<candies.length; i++) {
if(candies[i]+extraCandies >=max) {
result[i] = true
}
}
return result
};
网友评论