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
相关推荐
卷福同学2 小时前
不用服务器,不用配环境,我10分钟上线了一个AI Agent
人工智能·后端·算法
至乐活着4 小时前
深入解析跳表SkipList:原理、实现与性能优化实战
数据结构·算法·跳表·skiplist·java实现
Jerry5 小时前
LeetCode 383. 赎金信
算法
ai产品老杨5 小时前
H264 H265视频分析常见问题和排查清单
人工智能·算法·音视频
Jerry5 小时前
LeetCode 454. 四数相加 II
算法
可编程芯片开发6 小时前
基于CPS-SPWM链式STATCOM系统在电压不平衡环境下控制策略的simulink建模与仿真
算法
Jerry6 小时前
LeetCode 202. 快乐数
算法
hans汉斯7 小时前
基于改进交叉熵损失函数与Transformer的心电信号高风险分类研究
功能测试·深度学习·算法·yolo·目标检测·分类·transformer
Jerry7 小时前
LeetCode 349. 两个数组的交集
算法
YuK.W8 小时前
Leetcode100: 70.爬楼梯、118.杨辉三角、198.打家劫舍
java·算法·leetcode