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
相关推荐
小羊羔heihei11 分钟前
Python编程实战:12道趣味算法题
笔记·python·学习·其他·算法·学习方法·交友
三维重建-光栅投影17 分钟前
PCL之RANSAC实践
算法
weixin_4577600030 分钟前
深入解析 Beam Search:从原理到实践的高效解码算法
python·算法
ulimate_30 分钟前
anygrasp算法:调研与使用
算法
枳实-叶35 分钟前
50 道嵌入式音视频面试题
面试·职场和发展·音视频
愣头不青1 小时前
96.不同的二叉搜索树
数据结构·算法·leetcode
AI科技星1 小时前
光速螺旋量子几何统一场论——基于 v ≡ c 公理的四大基本力全维度求导证明与精准数值验证
c语言·开发语言·人工智能·算法·机器学习·平面
ab1515172 小时前
3.27完成3(指针)、13、41、44(指针)、50、51、95、96、97
算法
AI成长日志2 小时前
【强化学习专栏】深度强化学习技术演进:DQN、PPO、SAC的架构设计与训练优化
人工智能·算法·架构
郭逍遥2 小时前
[Godot] JPS跳点寻路和RVO避障
算法·godot·启发式算法