Leetcode75 – Kids with the Greatest Number of Candies #3

This is the 3rd problem in Leetcode75 ( Array&Hashing ).

Problem Statement

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 ext…


This content originally appeared on DEV Community and was authored by Dwiki

This is the 3rd problem in Leetcode75 ( Array&Hashing ).

Problem Statement

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.

Solution Strategy

  1. Create an array of boolean, we're going to store the result here
  2. Find the current maximum number of candies
  3. Iterate through the array of candies. For each candy, add the extra candies and check if this addition makes it the greatest. If so, append true to the results array; otherwise, append false
class Solution {
    func kidsWithCandies(_ candies: [Int], _ extraCandies: Int) -> [Bool] {
       var result = [Bool]()
       let greatestCandies = candies.max()

       guard let greatestCandies = greatestCandies else {
        fatalError("error")
       }

       for candy in candies {
        if candy + extraCandies >= greatestCandies {
            result.append(true)
        } else {
            result.append(false)
        }
       }

       return result
    } 
}

## SOLUTION 2 ##

if you are familiar with .map() function, you could do it like this

func kidsWithCandies(_ candies: [Int], _ extraCandies: Int) -> [Bool] {
    guard let maxCandies = candies.max() else { return [] }
    return candies.map { candy in
        candy + extraCandies >= maxCandies
    }
}


This content originally appeared on DEV Community and was authored by Dwiki


Print Share Comment Cite Upload Translate Updates
APA

Dwiki | Sciencx (2025-01-24T08:29:44+00:00) Leetcode75 – Kids with the Greatest Number of Candies #3. Retrieved from https://www.scien.cx/2025/01/24/leetcode75-kids-with-the-greatest-number-of-candies-3/

MLA
" » Leetcode75 – Kids with the Greatest Number of Candies #3." Dwiki | Sciencx - Friday January 24, 2025, https://www.scien.cx/2025/01/24/leetcode75-kids-with-the-greatest-number-of-candies-3/
HARVARD
Dwiki | Sciencx Friday January 24, 2025 » Leetcode75 – Kids with the Greatest Number of Candies #3., viewed ,<https://www.scien.cx/2025/01/24/leetcode75-kids-with-the-greatest-number-of-candies-3/>
VANCOUVER
Dwiki | Sciencx - » Leetcode75 – Kids with the Greatest Number of Candies #3. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/24/leetcode75-kids-with-the-greatest-number-of-candies-3/
CHICAGO
" » Leetcode75 – Kids with the Greatest Number of Candies #3." Dwiki | Sciencx - Accessed . https://www.scien.cx/2025/01/24/leetcode75-kids-with-the-greatest-number-of-candies-3/
IEEE
" » Leetcode75 – Kids with the Greatest Number of Candies #3." Dwiki | Sciencx [Online]. Available: https://www.scien.cx/2025/01/24/leetcode75-kids-with-the-greatest-number-of-candies-3/. [Accessed: ]
rf:citation
» Leetcode75 – Kids with the Greatest Number of Candies #3 | Dwiki | Sciencx | https://www.scien.cx/2025/01/24/leetcode75-kids-with-the-greatest-number-of-candies-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.