반응형
candies 배열과 extra 숫자가 주어질 때
배열의 각 숫자들이 extra 값을 더했을 때 배열에서 가장 큰 값이 될 수 있으면 true, 아니면 false를 반환하는 문제.
배열에서 최댓값 구하고
처음부터 돌면서 extra 더했을 때 최댓값과 비교한다.
class Solution {
public:
vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
vector<bool> ans;
int maxNum = 0;
for(int i = 0;i<candies.size();i++)
if(candies[i]> maxNum)
maxNum = candies[i];
for(int i = 0;i<candies.size();i++)
if(candies[i]+ extraCandies >= maxNum)
ans.push_back(true);
else
ans.push_back(false);
return ans;
}
};
반응형
'IT > 알고리즘' 카테고리의 다른 글
6] Leetcode 771. Jewels and Stones (0) | 2020.06.21 |
---|---|
5] Leetcode 1342. Number of Steps to Reduce a Number to Zero (0) | 2020.06.21 |
4] Leetcode 1108. Defanging an IP Address (0) | 2020.06.21 |
2] Leetcode 1470. Shuffle the Array (0) | 2020.06.16 |
1] Leetcode 1480. Running Sum of 1d Array (0) | 2020.06.16 |
댓글