Algorithm/LeetCode
[LeetCode][Easy][Java] 1431. Kids With the Greatest Number of Candies
youn12
2021. 1. 8. 17:58
✏️ Algorithm.
- 각 아이들(candies[index])에게 일정량의 캔디(extraCandies)를 주었을 때 전체 아이들중에서 가장 많은 캔디를 가지고 있는가?
📋 Solved.
1. 각각 가지고 있는 캔디(candeis[index]) 와 주어진 캔디(extraCandies)의 합계가 가지고있는 캔디의 최대개수(max)보다 크거나 같을 경우 true, 아닐 경우 false 리스트를 반환
✔️ Code.
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
List<Boolean> result = new ArrayList<Boolean>();
int max = 0;
for (int candy : candies) {
max = Math.max(candy, max);
}
for (int candy : candies) {
result.add((candy + extraCandies) >= max);
}
return result;
}