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
相关推荐
xie_pin_an4 分钟前
深入解析 C 语言排序算法:从快排优化到外排序实现
c语言·算法·排序算法
Hcoco_me8 分钟前
机器学习核心概念与主流算法(通俗详细版)
人工智能·算法·机器学习·数据挖掘·聚类
Hcoco_me9 分钟前
嵌入式场景算法轻量化部署checklist
算法
咸鱼加辣10 分钟前
【python面试】Python 的 lambda
javascript·python·算法
Jerryhut14 分钟前
sklearn函数总结十二 —— 聚类分析算法K-Means
算法·kmeans·sklearn
Swift社区34 分钟前
LeetCode 453 - 最小操作次数使数组元素相等
算法·leetcode·职场和发展
hoiii18740 分钟前
LR算法辅助的MIMO系统Zero Forcing检测
算法
糖葫芦君42 分钟前
Lora模型微调
人工智能·算法
小李小李快乐不已1 小时前
二叉树理论基础
数据结构·c++·算法·leetcode
仰泳的熊猫1 小时前
1149 Dangerous Goods Packaging
数据结构·c++·算法·pat考试