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 candiesi 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 resulti 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
相关推荐
罗湖老棍子3 小时前
【例 1】Sightseeing Trip(信息学奥赛一本通- P1494)(洛谷-P10927)
数据结构·算法·图论·最短路·floyd·最小环
文丑颜不良啊3 小时前
LeetCode201~225题目解析
java·leetcode
stsdddd5 小时前
YOLO算法建筑工地安全帽目标检测数据集16881张:标注核对笔记
算法·yolo·目标检测
CV爱数码6 小时前
论文精读|Learned Primal-Dual:把原始–对偶算法展开成网络
图像处理·算法·计算机视觉·ct·重建·断层扫描
yuniko-n6 小时前
【算法】约瑟夫环问题
java·算法
l1t7 小时前
测试DuckDB v2.1.0-alpha49002的bignum乘法
数据库·sql·算法·duckdb
Nil2087 小时前
LeetCode 283移动零
数据结构·算法·leetcode
CQU_JIAKE7 小时前
10.18【A】
算法
学顶教育7 小时前
学顶教育:公务员面试常见问题!
面试·职场和发展
LMT流氓兔8 小时前
第五单元——第4课:给工具参数加校验
java·前端·算法·ai·langchain