• This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Thank you very much for reading this article. Welcome [👍 like] [⭐ collect] [📝 comment] ~ Give up is not difficult, But insists it must be very cool ~ hope we all can be a little bit of progress every day ~ in this paper, from two headed white hat original ~ https://juejin.cn/user/2771185768884824/posts blog


1431. Kids with the most candy:

Candies [I] gives you an array of candies and an integer extraCandies, where candies[I] represents the number of candies owned by the ith child.

For each child, check to see if there is a scenario where the child has the most candy after distributing the extra extraCandies to the children. Note that more than one child is allowed to have the maximum number of sweets at the same time.

Sample 1

Input: candie's =,3,5,1,3 [2], extraCandies = 3 output: [true, true, true, false, true) : Child 1 has 2 sweets, and if he gets all the extra sweets (3), then he has a total of 5 sweets, and he will be the child with the most sweets. Child 2 has 3 sweets, and if he gets at least 2 extra sweets, he will be the child with the most sweets. Child 3 has five candies. He already has the most candies. Kid 4 has 1 candy, and even if he gets all the extra candy, he only has 4 candy, so he can't be the kid with the most candy. Child 5 has three sweets, and if he gets at least two extra sweets, he will be the child with the most sweets.Copy the code

The sample 2

Input: candie's =,2,1,1,2 [4], extraCandies = 1 output: [true, false, false, false, false) : There's only 1 extra candy, so no matter who gets the extra candy, only child 1 can be the one with the most candy.Copy the code

Sample 3

Input: candies = [12,1,12] extraCandies = 10 output: [true,false,true]Copy the code

prompt

  • 2 <= candies.length <= 100
  • 1 <= candies[i] <= 100
  • 1 <= extraCandies <= 50

Analysis of the

They want to know if there is a way for a child to have the most candy?

  • There are many ways to maximize the amount of candy a child can have with the extra candy. (The child may already have the most candy, so it doesn’t matter if he or she has extra candy; Or if the child already has a lot of candy, just give him a few and he can have the most, and the extra can be given to other children.
  • But the maximum amount of candy we can let a child have is the only result, is to give him all the extra candy, if we try our best to help him, he can’t be the maximum, then there is no way.

Answer key

java

class Solution {
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        // Find the maximum number of candies
        int max = 0;
        for (int c : candies) {
            max = Math.max(max, c);
        }
        
        // Suppose the extra candy goes to him
        List<Boolean> ans = new ArrayList<>(candies.length);
        for (int c : candies) {
            ans.add(c + extraCandies >= max);
        }
        
        returnans; }}Copy the code

c

/** * Note: The returned array must be malloced, assume caller calls free(). */
bool* kidsWithCandies(int* candies, int candiesSize, int extraCandies, int* returnSize){
    // Find the maximum number of candies
    *returnSize = candiesSize;
    int max = 0;
    for (int i = 0; i < candiesSize; ++i) {
        if(candies[i] > max) { max = candies[i]; }}// Suppose the extra candy goes to him
    bool *ans = (bool *) malloc(sizeof(bool) * candiesSize);
    for (int i = 0; i < candiesSize; ++i) {
        ans[i] = candies[i] + extraCandies >= max;
    }

    return ans;
}
Copy the code

c++

class Solution {
public:
    vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
        // Find the maximum number of candies
        int max = *max_element(candies.begin(), candies.end());

        // Suppose the extra candy goes to him
        vector<bool> ans;
        for (auto c : candies) {
            ans.push_back(c + extraCandies >= max);
        }
        
        returnans; }};Copy the code

python

class Solution:
    def kidsWithCandies(self, candies: List[int], extraCandies: int) - >List[bool] :
        # Find the maximum number of candies
        maxCandies = max(candies)
        # Assuming extra candy goes to him
        ans = [c + extraCandies >= maxCandies for c in candies]
        return ans
Copy the code

go

func kidsWithCandies(candies []int, extraCandies int) []bool {
    // Find the maximum number of candies
	max := 0
	for _, c := range candies {
		if c > max {
			max = c
		}
	}

	// Suppose the extra candy goes to him
	ans := make([]bool.len(candies))
	for i, c := range candies {
		ans[i] = c+extraCandies >= max
	}

	return ans
}
Copy the code

rust

impl Solution {
    pub fn kids_with_candies(candies: Vec<i32>, extra_candies: i32) - >Vec<bool> {
        // Find the maximum number of candies
        let max = candies.iter().max().unwrap();
        // Suppose the extra candy goes to him
        candies.iter().map(|c| {
            c + extra_candies >= *max
        }).collect()
    }
}
Copy the code


Portal: https://leetcode-cn.com/problems/kids-with-the-greatest-number-of-candies/


Please feel free to discuss in the comments section. The nuggets will draw 100 nuggets in the comments section after the diggnation project. See the event article for details