LeetCode Challenge: Kids With the Greatest Number of Candies

Will Sheppard
1 min readMay 6, 2021

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

Description

Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has.

For each kid check if there is a way to distribute extraCandies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies.

Solution:

Steps

1. loop through the array and store the highest value in the candies array, in a variable called greatest value
2. Map over the candies array, comparing each value in the sum of rach value in the array, plus the extra_candies, against the greatest_value variable, then true or false
3. return the new array

Code (Ruby)

Alternative Solution

Results

Runtime: 52 ms, faster than 74.87% of Ruby online submissions for Kids With the Greatest Number of Candies.Memory Usage: 209.8 MB, less than 99.47% of Ruby online submissions for Kids With the Greatest Number of Candies.

Thanks for viewing. If you have any feedback on how to improve this code, leave a comment.

--

--

Will Sheppard

Seasoned software engineer with 5+ years of experience in JavaScript, React, GraphQL, and AWS