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
相关推荐
Fanxt_Ja10 分钟前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下11 分钟前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶22 分钟前
算法 --- 字符串
算法
博笙困了33 分钟前
AcWing学习——差分
c++·算法
NAGNIP36 分钟前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP36 分钟前
大模型微调框架之LLaMA Factory
算法
echoarts37 分钟前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Python技术极客43 分钟前
一款超好用的 Python 交互式可视化工具,强烈推荐~
算法
徐小夕1 小时前
花了一天时间,开源了一套精美且支持复杂操作的表格编辑器tablejs
前端·算法·github
小刘鸭地下城1 小时前
深入浅出链表:从基础概念到核心操作全面解析
算法