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
相关推荐
ytttr8734 小时前
隐马尔可夫模型(HMM)MATLAB实现范例
开发语言·算法·matlab
AlenTech5 小时前
160. 相交链表 - 力扣(LeetCode)
数据结构·leetcode·链表
点云SLAM5 小时前
凸优化(Convex Optimization)理论(1)
人工智能·算法·slam·数学原理·凸优化·数值优化理论·机器人应用
jz_ddk5 小时前
[学习] 卫星导航的码相位与载波相位计算
学习·算法·gps·gnss·北斗
放荡不羁的野指针5 小时前
leetcode150题-动态规划
算法·动态规划
sin_hielo5 小时前
leetcode 1161(BFS)
数据结构·算法·leetcode
一起努力啊~5 小时前
算法刷题-二分查找
java·数据结构·算法
水月wwww6 小时前
【算法设计】动态规划
算法·动态规划
零售ERP菜鸟6 小时前
当业务战略摇摆不定:在变化中锚定不变的IT架构之道
信息可视化·职场和发展·架构·创业创新·学习方法·业界资讯
码农水水7 小时前
小红书Java面试被问:Online DDL的INSTANT、INPLACE、COPY算法差异
算法