본문 바로가기
알고리즘 문제/Leetcode

[Leetcode]1431. Kids With the Greatest Number of Candies

by 햄과함께 2023. 4. 17.
320x100

문제 : https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/description/


주어진 정수 배열 candies는 각각의 candies[i]가 i번째 아이가 가진 사탕 수를 나타냅니다. 또한, extraCandies라는 정수가 주어지는데, 이는 당신이 가진 추가 사탕 수를 나타냅니다.

n명의 아이가 있을 때, i번째 아이에게 extraCandies를 모두 준 후, i번째 아이가 모든 아이 중에서 가장 많은 사탕을 가지게 될 경우, 그 결과를 true로 하는 n개의 원소를 갖는 boolean 배열 result를 반환하세요. 만약 그렇지 않은 경우 false로 합니다.

여러 아이들이 가장 많은 사탕을 가질수도 있습니다.


candies를 탐색하면서 최대 캔디 수를 구합니다.

다시 candies를 탐색하면서 candies[i] + extraCandies 가 방금 구한 최대 캔디 수보다 같거나 크다면 true, 작다면 false가 됩니다.

 

시간복잡도는 O(N).


소스코드 : https://github.com/fpdjsns/Algorithm/blob/master/leetcode/easy/1431.%20Kids%20With%20the%20Greatest%20Number%20of%20Candies.cpp

320x100

댓글