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
相关推荐
WindSearcher1 小时前
大模型微调相关知识
后端·算法
取酒鱼食--【余九】1 小时前
rl_sar实现sim2real的整体思路
人工智能·笔记·算法·rl_sar
Magnum Lehar3 小时前
vulkan游戏引擎test_manager实现
java·算法·游戏引擎
水蓝烟雨3 小时前
[面试精选] 0094. 二叉树的中序遍历
算法·面试精选
超闻逸事3 小时前
【题解】[UTPC2024] C.Card Deck
c++·算法
暴力求解3 小时前
C++类和对象(上)
开发语言·c++·算法
JKHaaa4 小时前
几种简单的排序算法(C语言)
c语言·算法·排序算法
让我们一起加油好吗4 小时前
【基础算法】枚举(普通枚举、二进制枚举)
开发语言·c++·算法·二进制·枚举·位运算
FogLetter4 小时前
微信红包算法揭秘:从随机性到产品思维的完美结合
算法
BUG收容所所长4 小时前
二分查找的「左右为难」:如何优雅地找到数组中元素的首尾位置
前端·javascript·算法