fbaquant / LeetCode

1 stars 0 forks source link

Kids With the Greatest Number of Candies #116

Open juneharold opened 3 months ago

juneharold commented 3 months ago

We precompute the greatest number of candies that any kid(s) has, let's call it maxCandies.

Following the precomputation, we iterate over candies, checking whether the total candies that the current kid has exceeds maxCandies after giving extraCandies to the kid. For every kid, we perform candies[i] + extraCandies >= maxCandies and push it into a boolean list called result.

In the end, we return result.

class Solution(object):
    def kidsWithCandies(self, candies, extraCandies):
        # Find out the greatest number of candies among all the kids.
        maxCandies = max(candies)
        # For each kid, check if they will have greatest number of candies
        # among all the kids.
        result = []
        for i in range(len(candies)):            
            result.append(candies[i] + extraCandies >= maxCandies)
        return result