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
'Programming > LeetCode' 카테고리의 다른 글
| [Dart] LeetCode 2215. Find the Difference of Two Arrays (0) | 2025.05.07 |
|---|---|
| [Dart] 1732. Find the Highest Altitude (0) | 2025.05.02 |
| [Dart] 1207. Unique Number of Occurrences (0) | 2025.04.29 |
| [Dart] 1071. Greatest Common Divisor of Strings (0) | 2025.04.25 |
| [Dart] 872. Leaf-Similar Trees (0) | 2025.04.23 |