Search code examples
javascriptarray-algorithms

which Kid that has most candies algorithm problem


I am on leetcode working on this problem:

Given an 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.

Ex 1:

  • Input: candies = [2,3,5,1,3], extraCandies = 3
  • Output: [true,true,true,false,true]

Explanation:

  • Kid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies --- the greatest number of candies among the kids.
  • Kid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies.
  • Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.

I put console.log('true') & console.log('false') so i can see the true/false print whenever the code is succesfully executing its function. But it just prints true one time only.

Here is my code. Please tell me what I have wrong or what I am missing:

const candies = [2, 3, 5, 1, 3]
const extraCandies = 3;

 

var kidsWithCandies = (candies, extraCandies) => {
 
  var max = candies.reduce(function(a, b) {
    return Math.max(a, b);
  });

  var min = candies.reduce(function(a, b) {
    return Math.min(a, b);
  });
  console.log(min)
  console.log(max)

  for (let i = 0; i < candies.length; i++) {
    if (candies[i] === max) {
      return true
    } else if (candies[i] = candies[i] + extraCandies < max) {
      return false
    } else if (candies[i] = candies[i] + extraCandies > max) {
      return true
    } else if (candies[i] === min) {
      //> max
      candies[i] = candies[i] + extraCandies
      if (candies[i] > max) {
        return true
      } else if (candies[i] < max) {
        return false
      }

    }
  };
}


Solution

  • These'll pass in JavaScript:

    const kidsWithCandies = (candies, extraCandies) => {
        const maxCandies = Math.max(...candies);
        return candies.map(candy => candy + extraCandies >= maxCandies);
    };
    
    console.log(kidsWithCandies(candies = [2, 3, 5, 1, 3], extraCandies = 3))

    const kidsWithCandies = (candies, extraCandies) => {
        let maxCandies = 0;
        const greatest = [];
    
        for (const candy of candies) {
            (candy > maxCandies) && (maxCandies = candy);
        }
    
        for (let index = 0; index < candies.length; ++index) {
            greatest.push(candies[index] + extraCandies >= maxCandies);
        }
        return greatest;
    };
    
    console.log(kidsWithCandies(candies = [4,2,1,1,2], extraCandies = 1))

    Spread syntax (...) allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.

    Python

    class Solution:
        def kidsWithCandies(self, candies, extraCandies):
            max_candies = max(candies)
            return [candy + extraCandies >= max_candies for candy in candies]
    

    Java

    In Java, we'd use Arrays.stream:

    public final class Solution {
        public static final List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
            int maxCandies = Arrays.stream(candies).max().getAsInt();
            return Arrays.stream(candies).mapToObj(candy -> candy + extraCandies >= maxCandies).collect(Collectors.toList());
        }
    }
    
    

    The runtime is an order of N.


    References