Kids With the Greatest Number of Candies

array, puzzles, easy | 19 September 2022

This post is for my young sweet child, who likes candies and sweets.

This problem is Leetcode 1431. 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.

def kidsWithCandies(candies, extraCandies) -> List[bool]:
    gauge = max(candies) - extraCandies 
    return [candy >= gauge for candy in candies]

This solution can be expanded to running product, max, min.

from itertools import accumulate
def runningSum(A):
    return list(accumulate(A))
nums = [1,2,3,4]
print(runningSum(nums))

This solution can be expanded to running product using operator.mul(), max, min.

codeaccumulate.py
import operator
def runningProduct(A):
    return list(accumulate(A, operator.mul))
nums = [1,2,3,4]
print(runningProduct(nums))

def runningMax(A):
    return list(accumulate(A, max))
nums = [10,2,3,4]
print(runningMax(nums))
# [10, 10, 10, 10]

Reference

leetcode 1431. Kids With the Greatest Number of Candies