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
相关推荐
sali-tec41 分钟前
C# 基于halcon的视觉工作流-章29-边缘提取-亚像素
开发语言·图像处理·算法·计算机视觉·c#
屁股割了还要学2 小时前
【数据结构入门】堆
c语言·开发语言·数据结构·c++·考研·算法·链表
阿群今天学习了吗9 小时前
“鱼书”深度学习进阶笔记(3)第四章
人工智能·笔记·python·深度学习·算法
IT猿手9 小时前
2025年最新原创多目标算法:多目标酶作用优化算法(MOEAO)求解MaF1-MaF15及工程应用---盘式制动器设计,提供完整MATLAB代码
算法·数学建模·matlab·多目标优化算法·多目标算法
数据智能老司机13 小时前
图算法趣味学——最大流算法
数据结构·算法·云计算
秋难降13 小时前
【数据结构与算法】———深度优先:“死磕 + 回头” 的艺术
数据结构·python·算法
数据智能老司机13 小时前
图算法趣味学——图着色
数据结构·算法·云计算
数据智能老司机13 小时前
图算法趣味学——启发式引导搜索
数据结构·算法·云计算
John.Lewis14 小时前
数据结构初阶(8)二叉树的顺序结构 && 堆
c语言·数据结构·算法
SimonSkywalke14 小时前
基于知识图谱增强的RAG系统阅读笔记(七)GraphRAG实现(基于小说诛仙)(一)
算法