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
相关推荐
byzh_rc7 分钟前
[算法设计与分析-从入门到入土] 复杂算法
数据库·人工智能·算法·机器学习·支持向量机
Sunsets_Red12 分钟前
待修改莫队与普通莫队优化
java·c++·python·学习·算法·数学建模·c#
星火开发设计26 分钟前
深度优先搜索(DFS)详解及C++实现
c++·学习·算法·计算机·深度优先·大学生·期末考试
KingRumn41 分钟前
玩转DBus命令行工具之gdbus使用
linux·算法
二狗哈43 分钟前
Cesium快速入门34:3dTile高级样式设置
前端·javascript·算法·3d·webgl·cesium·地图可视化
@卞1 小时前
排序算法(2)--- 选择排序
算法·排序算法
脏脏a1 小时前
链式存储范式下的二叉树:基础操作实现解析
c语言·数据结构·算法·二叉树
iAkuya1 小时前
(leetcode)力扣100 26环状链表2(双指针)
算法·leetcode·链表
sin_hielo1 小时前
leetcode 2402(双堆模拟,小根堆)
数据结构·算法·leetcode
weixin_461769401 小时前
3. 无重复字符的最长子串
c++·算法·滑动窗口·最长字串