Programming/LeetCode
[Dart] LeetCode 1431. Kids With the Greatest Number of Candies
Meezzi
2025. 5. 1. 09:38
728x90
1. 문제
2. 요구사항
1) 각 아이에게 주어진 사탕의 개수를 나타내는 정수 배열 candies가 주어진다.
2) 각 아이에게 추가로 줄 수 있는 사탕의 개수를 나타내는 정수 extraCandies도 주어진다.
3) 각 아이가 가진 사탕의 수에 extraCandies를 더했을 때 그 아이가 가장 많은 사탕을 가진 아이 중 하나가 될 수 있는지 확인한다.
4) 각 아이에 대해 true 또는 false를 담은 배열을 반환한다.
5) 가장 많은 사탕을 가진 아이가 될 수 있다면 true, 그렇지 않다면 false를 반환한다.
3. 코드
class Solution {
List<bool> kidsWithCandies(List<int> candies, int extraCandies) {
// 현재 아이들 중 가장 많이 사탕을 가진 수를 찾는다.
int max = 0;
for (int i = 0; i < candies.length; i++) {
if (max < candies[i]) {
max = candies[i];
}
}
List<bool> result = [];
// 각 아이에 대해 extraCandies를 더했을 때 max와 같거나 많은지 확인
for (int i = 0; i < candies.length; i++) {
if (max > candies[i] + extraCandies) {
result.add(false);
} else {
result.add(true);
}
}
return result;
}
}
728x90