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

1431. 拥有最多糖果的孩子

小白渣翻译

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

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

你需要输出一个布尔型数组 result ,其中 resulti 是 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目录【基础版】

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

相关推荐
Nil2084 小时前
leetcode 763划分字母区间
算法·leetcode·职场和发展
不会就选b4 小时前
算法日常・每日刷题--<动态规划>20
算法·leetcode·动态规划
不会就选b5 小时前
算法日常・每日刷题--<动态规划>19
算法·leetcode·职场和发展
考研保研资料分享6 小时前
新闻传播保研经验:中传预推免面试、广告学代表作与实习准备(2025)
面试·职场和发展
垆边人似月.7 小时前
华为机试题 :和为 K 的子数组个数
算法·leetcode·华为
夏幻灵8 小时前
每日一题:LeetCode 165:比较版本号
算法·leetcode·职场和发展
Navigator_Z8 小时前
LeetCode //C - 1288. Remove Covered Intervals
c语言·算法·leetcode
0+1119 小时前
算法 --链表
数据结构·算法·leetcode·链表
圣保罗的大教堂9 小时前
leetcode 2333. 最小差值平方和 中等
leetcode
智码看视界10 小时前
C++智能指针全景剖析及面试20问
开发语言·c++·深度学习·面试·内存管理·智能指针