思路就是先跑一遍找到拥有最多的糖果有多少,再跑一遍看每个人加上额外糖果后能否超过最多的那个人的糖果.
C语言
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
bool* kidsWithCandies(int* candies, int candiesSize, int extraCandies, int* returnSize){
bool *r=(bool*)malloc(sizeof(bool)*candiesSize);
int max=0;
for(int i=0;i<candiesSize;i++)
if(max<candies[i])
max=candies[i];
for(int i=0;i<candiesSize;i++){
if(candies[i]+extraCandies>=max)
r[i]=true;
else
r[i]=false;
}
*returnSize=candiesSize;
return r;
}
JAVA
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
List<Boolean> r=new ArrayList<>();
int max=0;
for(int i=0;i<candies.length;i++)
if(max<candies[i])
max=candies[i];
for(int i=0;i<candies.length;i++)
r.add(candies[i]+extraCandies>=max);
return r;
}
}