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
相关推荐
Code-keys10 分钟前
Android Codec2 Filter 算法模块开发指南
android·算法·音视频·视频编解码
无忧智库23 分钟前
低空经济新基建:构建低空飞行大数据中心与行业应用算法工厂的全景式蓝图(WORD)
算法
闻缺陷则喜何志丹1 小时前
【背包 组合】P7552 [COCI 2020/2021 #6] Anagramistica|普及+
c++·算法·背包·洛谷·组合
一只机电自动化菜鸟2 小时前
一建机电备考笔记(17) 常用设备—通用设备1(含考频+题型)
笔记·学习·职场和发展·生活·学习方法
小章UPUP2 小时前
2026年第十六届MathorCup数学应用挑战赛D题国奖思路
算法
hssfscv2 小时前
软件设计师下午试题四——C语言(N皇后问题、分治、动态规划)
c语言·算法·动态规划
lolo大魔王2 小时前
Go语言的反射机制
开发语言·后端·算法·golang
白羊by2 小时前
Softmax 激活函数详解:从数学原理到应用场景
网络·人工智能·深度学习·算法·损失函数
故事和你913 小时前
洛谷-算法1-7-搜索3
数据结构·c++·算法·leetcode·动态规划
chipsense3 小时前
霍尔电流传感器选型方法论再升级:从800V平台到TMR竞争的全场景决策树
算法·决策树·机器学习·闭环霍尔·tmr传感