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

2024. 7. 25. 11:47Algorithm

1. 문제 :

https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/description/?envType=study-plan-v2&envId=leetcode-75

 

2. 풀이 : 

class Solution:
    def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
        if not candies:
            return []
        max_candies = max(candies) 
        for i in range(len(candies)):
            if candies[i] + extraCandies >= max_candies:
                candies[i] = True
            else:
                candies[i] = False 
        return candies

 

max()라는 method를 이용해서 최대 캔디 수를 구한다. 이제 loop를 돌면서 extra candy + current_candy 가 max candy보다 크거나 같은지를 판단하고 맞다면 candies [i]를 True로 아니라면 False로 설정한다.