Leetcode - Kids with the greates number of candies solution
Published: 2023-07-11 13:27:24

---
title: Leetcode - Kids with the greates number of candies solution
published: true
description: 
tags: 
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-07-11 13:27 +0000
---


Original post: [brsjsk.github.io] (https://brsjsk.github.io/posts/leetcode-greatest-number-of-candies)
Github [url](https://github.com/BrsJsk/2-kids-with-the-greatest-number-of-candies)
Leetcode [url](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/)

---

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.

Example 1:

```
Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation: If you give all extraCandies to:
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
```

---

# Solution 1

```
/**
 * @param {number[]} candies
 * @param {number} extraCandies
 * @return {boolean[]}
 */
module.exports.kidsWithCandies = (candies, extraCandies) => {
    const largestNumber = Math.max(...candies);

    return candies.map(candie => candie + extraCandies >= largestNumber);
};
```

This is a very simple one-liner solution..but, it's slower to execute then a good old for loop.

```
Runtime: 136 ms, faster than 5.86% of JavaScript online submissions for Kids With the Greatest Number of Candies.
Memory Usage: 36.7 MB, less than 10.89% of JavaScript online submissions for Kids With the Greatest Number of Candies.
```

# Solution 2

```
/**
 * @param {number[]} candies
 * @param {number} extraCandies
 * @return {boolean[]}
 */
module.exports.kidsWithCandies = (candies, extraCandies) => {
    const largestNumber = Math.max(...candies);
    let items = [];

    for (let i = 0; i < candies.length; i++) {
        items.push(candies[i] + extraCandies >= largestNumber)
    }

    return items;
};
```

```
Runtime: 76 ms, faster than 46.11% of JavaScript online submissions for Kids With the Greatest Number of Candies.
Memory Usage: 37.1 MB, less than 7.66% of JavaScript online submissions for Kids With the Greatest Number of Candies.
```

# Difference

Difference in execution speed is quite noticable.
![](https://brsjsk.github.io/static/4a0ca2e0091c77554423ffa8d47e2d19/e751c/ms_speed.png)

But difference in memory usage? Ehh, not so much noticable..
![](https://brsjsk.github.io/static/73d9f1c68de3593cd061d28e1f4fd623/62a6a/mb_usage.png)