DEV Community

bengineer
bengineer

Posted on

Kids With the Greatest Number of Candies (ruby LeetCode)

Description: https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/submissions/

Solution:

def kids_with_candies(candies, extra_candies)
    greatest = candies.max

    candies.map do |candy|
        (candy + extra_candies) >= greatest
    end
end
Enter fullscreen mode Exit fullscreen mode

Explanation: A kid will have the greatest number of candies if they have the same amount or greater, than the kid that already have the most amount of candies. So the first step is to find the greatest amount of candy, among the kids and save that number. The second step is to map through the candies array and see if the selected amount, plus the extra_candies, is equal or greater than the number we saved on the first step. In the end, the function will return an array with true and/or false values.

Top comments (0)