Leetcode 1431. Kids With the Greatest Number of Candies

Problem

There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.

Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.

Note that multiple kids can have the greatest number of candies.

Algorithm

First, calculate the maximum number of candies; then, iterate through each child's candies + extraCandies, determining if it becomes the maximum.

Code

python3 复制代码
class Solution:
    def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
        max_val = max(candies)
        ans = []
        for candy in candies:
            if max_val <= candy + extraCandies:
                ans.append(True)
            else:
                ans.append(False)
        return ans
相关推荐
naturerun4 小时前
从数组中删除元素的算法
数据结构·c++·算法
he___H4 小时前
接雨水----解
leetcode
斯内科5 小时前
四胞胎素数:找出‌个位数分别是 1、3、7、9‌,且‌十位及更高位数字完全相同‌的质数,例如 11、13、17、19
算法·质数·素数·四胞胎素数
Hello.Reader6 小时前
算法基础(十二)——主方法:快速求解常见递归式
算法
香蕉鼠片6 小时前
面试过程中被问懵
面试·职场和发展
小O的算法实验室6 小时前
2026年IEEE TITS,面向按需外卖配送调度的特定问题知识与基于学习元启发式算法,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
加勒比海带666 小时前
目标检测算法——农林行业数据集汇总附下载链接【Plant】
大数据·图像处理·人工智能·算法·目标检测
洛水水6 小时前
【力扣100题】23. 螺旋矩阵
算法·leetcode·矩阵
影sir7 小时前
不同测试数据下,该如何选择算法
算法·深度优先
潇湘散客7 小时前
CAX软件插件化设计实现牛刀小试
c++·算法·图形学·opengl