Study/Algorithm
[Leetcode] 1431. Kids with the greatest number of candies (C++ 풀이)
shan00
2025. 1. 6. 05:36
얘도 easy peasy 문제입니다. n개로 정수로 이루어진 배열이 주어집니다. 여기에 각 kid가 extraCandies를 가졌을 때, 해당 kid가 주어진 배열 내에서 가장 큰 수를 가지는지? 판단하는 문제입니다. 배열을 활용해보는 기본적인 문제입니다.
1. 제출 코드
class Solution {
public:
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
vector<bool> result;
int cur_max = *max_element(candies.begin(), candies.end());
for (int i = 0; i < candies.size(); i++){
if (candies[i]+extraCandies >= cur_max){
result.push_back(true);
}
else{
result.push_back(false);
}
}
return result;
}
};
주어진 candies에서 최대값을 미리 구합니다. 그리고 하나씩 kid가 가진 candies에 extraCandies를 더해가며, 해당 합한 숫자가 cur_max보다 같거나 크다면 true, 그렇지 않다면 false를 최종 배열에 추가해줍니다.