{ glenmccallumcan }

LeetCode 1431. Kids With the Greatest Number of Candies

Mar 14, 2022
3 minutes

I’ve been playing with javascript lately. One of the things I’ve been doing is leetcode excercises to familiarize myself with the usage of new syntax. Starting from the easy problems and working my way towards more difficult problems.

I usually solve the problem fairly quickly using non-javascript specific (sometimes awkward) code. Then I check the community solutions for the most elegant solution I can find and analyze it.

The Problem

There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.

Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.

Note that multiple kids can have the greatest number of candies.

My Naive Solution

I couldn’t think of a clever way to solve this one so I checked the hint. It suggested a greedy solution so I jumped to brute force.

I made two iterations of the array. The first iteration to get the max value and the second iteration to determine if each element (with extraCandies) is greater than the max value.

/**
 * @param {number[]} candies
 * @param {number} extraCandies
 * @return {boolean[]}
 */
var kidsWithCandies = function(candies, extraCandies) {
    let max = 0;
    
    for(let i = 0; i< candies.length; i++)
        {
            max = candies[i] > max ? candies[i] : max;
        }
    
    let result = [];
    for(let i = 0; i< candies.length; i++)
        {
            if(candies[i] + extraCandies >= max)
                {
                    result[i] = true;
                }
            else{
                result[i] = false;
            }
        }
    return result;
};

The Elegant Solution

The elegant solution is basically the same approach I used except they take advantage of a built in libray to get the max value. Then they use a built in feature, map, to generate the boolean array of results.

There are three new things I learned from this solution.

  1. Math.max - built in library. Got it.
  2. … the spread syntax. The breaks the array into discrete arguments where multiple parameters are expected. If the function signature only has 2 parameters and the spreaded array has 6 elements it will execute but only the first 2 elements of that array will be used in the function. See also rest parameters
  3. Array map method - creates a new array by performing a function on each array element. This is really common to C# syntax. It just has looser typing.
/**
 * @param {number[]} candies
 * @param {number} extraCandies
 * @return {boolean[]}
 */
var kidsWithCandies = function(candies, extraCandies) {
    let max = Math.max(...candies);
    return candies.map(item => item + extraCandies >= max);
};