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

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的理解。

小白:嘿嘿,这不巧了么这不是。

java 复制代码
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目录【基础版】

编码道路漫漫,只要先看脚下的路,徐徐前进即可。

相关推荐
音符犹如代码2 小时前
Java并发List实战:CopyOnWriteArrayList原理与ArrayList常见面试题
java·开发语言·面试·list
sin_hielo2 小时前
leetcode 1611
算法·leetcode
来荔枝一大筐3 小时前
C++ LeetCode 力扣刷题 541. 反转字符串 II
c++·算法·leetcode
小白程序员成长日记5 小时前
2025.11.07 力扣每日一题
数据结构·算法·leetcode
·白小白5 小时前
力扣(LeetCode) ——209. 长度最小的子数组(C++)
c++·算法·leetcode
小白程序员成长日记6 小时前
2025.11.08 力扣每日一题
算法·leetcode·职场和发展
蚂小蚁7 小时前
一文吃透:宏任务、微任务、事件循环、浏览器渲染、Vue 批处理与 Node 差异(含性能优化)
前端·面试·架构
他们叫我一代大侠7 小时前
Leetcode :模拟足球赛小组各种比分的出线状况
算法·leetcode·职场和发展
海琴烟Sunshine8 小时前
leetcode 345. 反转字符串中的元音字母 python
python·算法·leetcode
uhakadotcom8 小时前
在使用cloudflare workers时,假如有几十个请求,如何去控制并发?
前端·面试·架构