Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1431. Kids With the Greatest Number of Candies - Leetcode Solution

Problem Description

You are given an array candies where each element represents the number of candies that each kid has. You are also given an integer extraCandies, which represents the number of extra candies you can give to any one kid.

For each kid, determine if giving them extraCandies will result in them having the greatest number of candies among all kids (i.e., at least as many candies as any other kid).

Return a list of boolean values, where each element corresponds to a kid and is True if, after receiving extraCandies, they have the greatest number of candies, or False otherwise.

  • Each kid can only receive the extra candies once.
  • Every element in candies is a non-negative integer.
  • You must check for every kid independently.

Thought Process

To solve this problem, let's first think about what it means for a kid to have the "greatest" number of candies. After giving a kid the extra candies, we need to compare their new total with the current maximum number of candies any kid has.

A brute-force approach would be to, for each kid, add extraCandies to their count and compare with every other kid's count. However, this would be inefficient as it requires nested loops.

We can optimize by first determining the current maximum number of candies any kid has. Then, for each kid, we simply check if their candies plus extraCandies is at least as large as this maximum. This reduces the need for repeated comparisons.

This approach is both efficient and easy to implement.

Solution Approach

  • Step 1: Find the maximum number of candies any kid currently has. This can be done with a simple pass through the candies array.
  • Step 2: For each kid, add extraCandies to their current count and check if this sum is greater than or equal to the maximum from Step 1.
  • Step 3: Collect the results in a boolean list, where each entry corresponds to whether that kid can have the greatest number of candies.

This method is efficient because we only traverse the list twice: once to find the maximum, and once to compute the results.

Example Walkthrough

Let's use the following example:
candies = [2, 3, 5, 1, 3], extraCandies = 3

  1. Find the maximum: The largest number in candies is 5.
  2. Check each kid:
    • Kid 0: 2 + 3 = 5 (equal to max) → True
    • Kid 1: 3 + 3 = 6 (greater than max) → True
    • Kid 2: 5 + 3 = 8 (greater than max) → True
    • Kid 3: 1 + 3 = 4 (less than max) → False
    • Kid 4: 3 + 3 = 6 (greater than max) → True
  3. Result: [True, True, True, False, True]

This shows how each kid's candies plus the extra is compared to the maximum, and the result is determined accordingly.

Time and Space Complexity

  • Brute-force Approach: For each kid, compare their new total with every other kid (O(n2)), which is inefficient for large inputs.
  • Optimized Approach:
    • Finding the maximum takes O(n) time.
    • Checking each kid's result also takes O(n) time.
    • Total time complexity: O(n)
    • Space complexity: O(n) for the result list.

The optimized approach is both time and space efficient.

Code Implementation

class Solution:
    def kidsWithCandies(self, candies, extraCandies):
        max_candies = max(candies)
        return [(candy + extraCandies) >= max_candies for candy in candies]
      
class Solution {
public:
    vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
        int max_candies = *max_element(candies.begin(), candies.end());
        vector<bool> result;
        for (int candy : candies) {
            result.push_back(candy + extraCandies >= max_candies);
        }
        return result;
    }
};
      
import java.util.*;
class Solution {
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        int max = 0;
        for (int c : candies) {
            if (c > max) max = c;
        }
        List<Boolean> result = new ArrayList<>();
        for (int c : candies) {
            result.add(c + extraCandies >= max);
        }
        return result;
    }
}
      
var kidsWithCandies = function(candies, extraCandies) {
    const max = Math.max(...candies);
    return candies.map(candy => (candy + extraCandies) >= max);
};
      

Summary

The key to solving the "Kids With the Greatest Number of Candies" problem efficiently is to first determine the maximum candies any kid has, then for each kid, check if giving them the extra candies would match or exceed this maximum. This approach avoids unnecessary nested loops, resulting in an O(n) solution that is both simple and effective.

The solution demonstrates how understanding the problem's core requirement allows us to optimize and write concise code, making it a great example of how to approach array-based comparison problems.