小白水平理解面试经典题目1431. Kids With the Greatest Number of Candies【Array类】

文章讲述了如何解决LeetCode中的一个问题,即在给每个孩子增加一定数量的糖果后,找出哪些孩子拥有最多的糖果。作者通过实例和代码解析了如何使用数组和循环来判断每个孩子的糖果数量是否达到最多。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人专栏
🤺 leetcode
🧗 Leetcode Prime
🏇 Golang20天教程
🚴‍♂️ Java问题收集园地
🌴 成长感悟
欢迎大家观看,不执着于追求顶峰,只享受探索过程

1431. 拥有最多糖果的孩子

小白渣翻译

一群孩子手里拿 着不同数目的糖果。你打算额外给每个孩子一些糖果,然后再确定哪些孩子拥有最多的糖果。

给你一个数组 candies ,其中 candies[i] 代表第 i 个孩子拥有的糖果数目。另给你一个整数 extraCandies ,代表你要额外给每个孩子增加的糖果数目。

你需要输出一个布尔型数组 result ,其中 result[i] 是 true 的话,表示第 i 个孩子拥有最多 的糖果;否则为 false 。

Example 1:

Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation: If you give all extraCandies to:

  • Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
  • Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
  • Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
  • Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
  • Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.

Example 2:

Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]
Explanation: There is only 1 extra candy.
Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.

Example 3:

Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]

小白理解过程

“越过绵绵的高山,越过无尽的沧海”,带着耳机刷题的小白边听歌边做题。

这时候黑长直女神过来问:小白,你这题怎么思考的啊?感觉这道小朋友分糖果的题你看到了吗?

小白内心镇定:小美,《凤凰传奇》演唱会有机会一起去听吧?
在这里插入图片描述
哦,不是的!其实这样的话你理解起来就简单多了,用三部曲就是这么来理解。
咱们拿第一个例子来辅助解释。candies = [2,3,5,1,3], extraCandies = 3

  • 3 个额外的糖果能让第一个孩子拥有最多的糖果。
  • 2 个额外的糖果能让第二个、和第五个孩子拥有最多的糖果。
  • 第四个孩子即使拥有所有的额外糖果,也无法拥有最多的糖果。
    其实可能这道题给你造成的困扰就是这个最大数的问题,我们可以理解为大于等于这个数组中的最大数就是我们要找的为true的值。

小美:小伙子,可以啊,这三段式解法还是很清晰啊!不过凤凰传奇要是你买票我倒是可以考虑去哦。小美一转头,黑发飘逸而下,急匆匆走出了自习室。

小白:嘿嘿,这是同意了啊

在这里插入图片描述

面试环节
面试官:你可以解答这道”拥有最多糖果的孩子“的题目吗,来看看小伙子你对array的理解。

小白:嘿嘿,这不巧了么这不是。
在这里插入图片描述

public static boolean compare(int[] candiesNew, int kidsWithExtra) {
        int max = candiesNew[0];
        boolean kidIsMax = false;
        for (int i = 1; i < candiesNew.length; i++) {
            if (candiesNew[i] > max) {
                max = candiesNew[i];
            }
        }
        if (kidsWithExtra >= max) {
            kidIsMax = true;
        }
        return kidIsMax;
    }

    public static List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        List<Boolean> res = new ArrayList<>();
        int[] candiesNew = Arrays.copyOf(candies, candies.length);
        for (int i = 0; i < candies.length; i++) {
            int kidsWithExtra = candies[i] + extraCandies;
            candiesNew[i] = kidsWithExtra;
            boolean kidIsMax = compare(candiesNew, kidsWithExtra);
            res.add(kidIsMax);
            candiesNew = Arrays.copyOf(candies, candies.length);
        }
        return res;
    }

小明:OK,完事儿,等着面试官来表扬自己吧。他肯定会说:小子,你是个好手!工位都给你准备好了,工资你说了算。

面试官:矮油,不错啊,不过这个速度咱们是不是还有其他考虑啊。

小明OS:今年这个找工市场,人言洛阳花似锦,偏我来时不逢春。。。不是,这面试官好体力啊!

在这里插入图片描述

============================================================================
🍀🍀🍀🍀🍀🍀更多算法题解请看 面试数据结构与算法总结分类+leetcode目录【基础版】
编码道路漫漫,只要先看脚下的路,徐徐前进即可。

The Power of Reading Reading is a powerful tool that allows individuals to expand their knowledge and understanding of the world around them. Through books, articles, and other written material, people can explore different perspectives, cultures, and concepts. Reading also has the ability to enhance an individual's vocabulary, critical thinking, and communication skills. One of the greatest benefits of reading is its ability to spark imagination and creativity. When a person reads a story, they create vivid images in their mind, bringing the words to life. This can lead to a greater appreciation for art and beauty, as well as an understanding of different creative processes. Reading can also have a profound impact on an individual's mental health. Studies have shown that reading can reduce stress, increase empathy, and improve sleep. It can also serve as a form of escapism, allowing individuals to take a break from the rigors of daily life. Furthermore, reading can be a powerful tool for personal growth and development. By reading about different cultures, historical events, and other topics, individuals can expand their perspective and gain a deeper understanding of the world around them. This can lead to greater empathy and a more open-minded outlook on life. In conclusion, the power of reading cannot be understated. It has the ability to entertain, educate, and inspire individuals of all ages and backgrounds. Whether it is through novels, non-fiction, or other written materials, reading has the potential to make a significant impact on the world and the people who engage with it.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值