[Leetcode] 1431. Kids With the Greatest Number of Candies
문제 정의
캔디를 가지고 있는 n개의 아이들이 있다. candies[i] 는 i번째 아이가 가지고 있는 캔디의 숫자이고, extraCandies 는 당신이 가지고 있는 캔디의 수다.
i번째 아이에게 extraCandies를 전부 다 준다고 했을 때, 그 아이가 모든 아이 중 가장 많은 캔디를 가지고 있는 것인지 result[i] 에 true/false를 담은 array를 반환하라.
https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/
Kids With the Greatest Number of Candies - LeetCode
Can you solve this real interview question? Kids With the Greatest Number of Candies - There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandie
leetcode.com
풀이
역시 easy 문제ㅠ 오랜만에 푸는데 순간 sort 해야하는줄 알고 당황했는데 가장 큰값만 알고 있으면 candies[i] + extrasCandies 값이랑 max 값을 비교해주면 true/false 값을 판단할 수 있다.
와 근데 다른 풀이보니까 Stream 으로 엄청 간단하게 풀었네.. 이렇게 좀 풀어봐야겠다.
코드
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int max = 0;
for (int i=0; i<candies.length; i++) {
if (max < candies[i]) {
max = candies[i];
}
}
List<Boolean> result = new ArrayList();
for (int i=0; i<candies.length; i++) {
if (candies[i] + extraCandies >= max) {
result.add(true);
} else {
result.add(false);
}
}
return result;
}
}