题目链接:https://leetcode-cn.com/problems/kids-with-the-greatest-number-of-candies/
题目描述
分析
1. 遍历
从candies中找到最大的,再遍历candies,若元素加额外数>=最大数,则为此孩子有最多的糖果
代码实现
public enum Q1431 {
instance;
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int max=Integer.MIN_VALUE;
for(int candy:candies){
max=max<candy?candy:max;
}
List<Boolean> list = new ArrayList<>(candies.length);
for(int candy:candies){
if(candy+extraCandies>=max){
list.add(true);
}else {
list.add(false);
}
}
return list;
}
public static void main(String[] args) {
int[] candies=new int[]{4,2,1,1,2}; int extraCandies=1;
System.out.println(Q1431.instance.kidsWithCandies(candies,extraCandies));
}
}