↓Skip to main content
  1. LeetCode/

LeetCode 1431: Kids With the Greatest Number of Candies

·1 min· ·
LeetCode Easy Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-03-18

  • Submissions: Wrong Answer → Accepted ×2

Approach
#

Give all the extra candies to one kid; they have the most if they reach at least the original maximum (Array Template 3). Several kids can tie for the most, so a tie counts.

My Attempts
#

Attempt 1 used >, so ties did not count. For candies = [3], extraCandies = 0, the only kid already has the most, so the answer is [True]; it returned [False]. Changing to >= passed; the third submission moved max(candies) out of the loop.

Solution
#

class Solution:
    def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
        top = max(candies)
        return [c + extraCandies >= top for c in candies]

Complexity
#

Time O(n), space O(n) for the output.

Mistakes to Avoid
#

  • Use >=; a tie counts as the most.
  • Compute the maximum once instead of inside the comprehension (that would be O(n²)).

Interview Explanation
#

I compute the maximum once; each kid with the extra candies is True when they are no less than it, ties included. Time O(n).

Related

LeetCode 3487: Maximum Unique Subarray Sum After Deletion
·2 mins
LeetCode Easy Greedy Hash-Table Array
LeetCode 3487 note: free deletion means picking distinct values; take every non-negative one, or only the maximum when all are negative
LeetCode 3131: Find the Integer Added to Array I
·1 min
LeetCode Easy Array
LeetCode 3131 note: every element gets the same x, so x is the difference of the two minimums (or of the sums divided by the length)
LeetCode 3074: Apple Redistribution into Boxes
·1 min
LeetCode Easy Array Greedy Sorting
LeetCode 3074 note: apples can be split freely, so take boxes from the largest capacity until the total fits every apple
LeetCode 2099: Find Subsequence of Length K With the Largest Sum
·1 min
LeetCode Easy Array Hash-Table Sorting Heap-Priority-Queue
LeetCode 2099 note: pick the k largest values along with their indices, then sort by index to restore the original order
LeetCode 2335: Minimum Amount of Time to Fill Cups
·1 min
LeetCode Easy Array Greedy Sorting Heap-Priority-Queue
LeetCode 2335 note: the answer is max(the largest count, ceil(total / 2)); simulating means filling one cup from each of the two largest every second
LeetCode 1464: Maximum Product of Two Elements in an Array
·1 min
LeetCode Easy Array Sorting Heap-Priority-Queue
LeetCode 1464 note: all values are positive, so take the two largest a and b and return (a - 1)(b - 1)