Um momento
Leetcode / 1431. Kids With the Greatest Number of Candies

Pick a programming language:

Here is the source code for the solution to this problem.

class Solution {
    // time O(n)
    // space O(n)
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        List<Boolean> result = new ArrayList<Boolean>(candies.length);

        // First the greatest number of candies a kid has
        int greatest = candies[0];
        for (int i = 1; i < candies.length; i++) {
            if (candies[i] > greatest) {
                greatest = candies[i];
            }
        }

        for (int i = 0; i < candies.length; i++) {
            boolean hasGreatest = candies[i] + extraCandies >= greatest;
            result.add(hasGreatest);
        }

        return result;
    }
}
public class Solution {
    public IList<bool> KidsWithCandies(int[] candies, int extraCandies) {
        int greatest = 0;
        IList<bool> result = new List<bool>(candies.Length);
        
        foreach (int candyCount in candies) {
            if (candyCount > greatest) {
                greatest = candyCount;
            }
        }

        foreach (int candyCount in candies) {
            result.Add(candyCount + extraCandies >= greatest);
        }

        return result;
    }
}
Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: