[LeetCode]#1431. Kids With the Greatest Number of Candies

Fatboy Slim
1 min readMay 5, 2020

--

Environment: Python 3.7

Key technique: append

Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has.

For each kid check if there is a way to distribute extraCandies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies.

Example 1:

Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]

Analysis:

  1. Use max function to find maximum number in list.
  2. If current number add extraCandies > maximum number, return True.
  3. Else, return False.

Solution:

class Solution:
def kidsWithCandies(self, candies, extraCandies):
l=len(candies)
ans=[]
for i in range(l):
if candies[i]+extraCandies >= max(candies):
ans.append(True)
else:
ans.append(False)
return ans

Submissions:

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

Responses (2)