Likou 1431-The child with the most candies

topic

Give you an array candies and an integer extraCandies, where candies[i] represents the number of candies owned by the i-th child.

For each child, check whether there is a plan. After allocating extra Candies to the children, this child has the most candies. Note that multiple children are allowed to have the maximum number of candies at the same time.

Example:

 示例1:
 输入:candies = [2,3,5,1,3], extraCandies = 3
 输出:[true,true,true,false,true] 
 解释:
 孩子 1 有 2 个糖果,如果他得到所有额外的糖果(3个),那么他总共有 5 个糖果,他将成为拥有最多糖果的孩子。
 孩子 2 有 3 个糖果,如果他得到至少 2 个额外糖果,那么他将成为拥有最多糖果的孩子。
 孩子 3 有 5 个糖果,他已经是拥有最多糖果的孩子。
 孩子 4 有 1 个糖果,即使他得到所有额外的糖果,他也只有 4 个糖果,无法成为拥有糖果最多的孩子。
 孩子 5 有 3 个糖果,如果他得到至少 2 个额外糖果,那么他将成为拥有最多糖果的孩子。

 示例2:
 输入:candies = [4,2,1,1,2], extraCandies = 1
 输出:[true,false,false,false,false] 
 解释:只有 1 个额外糖果,所以不管额外糖果给谁,只有孩子 1 可以成为拥有糖果最多的孩子。

Ideas:

Traverse the array, take each number in the array plus the extra number of candies, and compare its value with the largest number in the array.

Code:

class Solution {
    
    
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
    
    
        List<Boolean> booleanList = new ArrayList<>();
        int max = Arrays.stream(candies).max().getAsInt();
        for (int i = 0; i < candies.length; i++) {
    
    
            int arr = candies[i] + extraCandies;

            if(arr >= max ){
    
    
                booleanList.add(i,true);
            }else
                booleanList.add(i,false);
        }
        return booleanList;

    }
    }

effect:

Insert picture description here

Thinking:

1 The maximum value in the array can be taken using a stream:

int max = Arrays.stream(arr).max().getAsInt();

2 The efficiency is not very high,

Reference official code:

class Solution {
    
    
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
    
    
        int n = candies.length;
        int maxCandies = 0;
        for (int i = 0; i < n; ++i) {
    
    
            maxCandies = Math.max(maxCandies, candies[i]);
        }
        List<Boolean> ret = new ArrayList<Boolean>();
        for (int i = 0; i < n; ++i) {
    
    
            ret.add(candies[i] + extraCandies >= maxCandies);
        }
        return ret;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/kids-with-the-greatest-number-of-candies/solution/yong-you-zui-duo-tang-guo-de-hai-zi-by-leetcode-so/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

My own modified code

class Solution {
    
    
    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
    
    
    
        List<Boolean> booleanList = new ArrayList<>();
        int maxCandies = 0;
         for (int i = 0; i < candies.length; ++i) {
    
    
            maxCandies = Math.max(maxCandies, candies[i]);
        }
        for (int i = 0; i < candies.length; i++) {
    
    
            
            int arr = candies[i]+extraCandies;
            if(arr >= maxCandies ){
    
    
                booleanList.add(i,true);
            }else
                booleanList.add(i,false);
        }

        return booleanList;
    }
    }

The efficiency has improved a lot:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42898315/article/details/108486231