LeetCode1431.拥有最多糖果的孩子

儿童节快乐

题目要求

 

 

算法分析

贪心算法,找到最大值,用最大值减去额外糖果数量,小于这个结果的不可能获得最多糖果

代码展示(C#)

public class Solution {
    public IList<bool> KidsWithCandies(int[] candies, int extraCandies) {
        List<bool> ret = new List<bool>();
        int max = 0;
        for(int i = 0; i < candies.Length; i++){
            if(max < candies[i]){max = candies[i];}
        }
        for(int i = 0; i < candies.Length; i++){
            ret.Add((max - extraCandies) <= candies[i]);
        }
        return ret;
    }
}

 

提交结果

 

posted on 2020-06-01 23:11  King__R  阅读(157)  评论(0编辑  收藏  举报