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 candiesi 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 resulti 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
相关推荐
凯瑟琳.奥古斯特8 小时前
力扣1235:加权区间调度最优解
java·python·算法·leetcode·职场和发展
耶叶8 小时前
餐厅出入最少人数问题:贪心算法
算法·贪心算法
gihigo19988 小时前
基于小波框架与稀疏表示的SAR图像目标识别系统(MATLAB实现)
算法
吴可可1238 小时前
CAD2004自定义实体开发环境配置
c++·算法
装不满的克莱因瓶8 小时前
矩阵的主成分是什么?主成分分析(PCA)又能做什么?
人工智能·线性代数·算法·机器学习·ai·矩阵·pca
大菜菜小个子8 小时前
template<typename T>使用
java·开发语言·算法
Fanfanaas8 小时前
C++ 继承
java·开发语言·jvm·c++·学习·算法
lqqjuly9 小时前
模型合并与融合:理论、算法与可运行实现—从损失曲面几何到多模型融合
算法
memcpy09 小时前
LeetCode 2144. 打折购买糖果的最小开销【贪心】
算法·leetcode·职场和发展
Purple Coder10 小时前
STM32基础(1)
职场和发展