[LeetCode]#1431. Kids With the Greatest Number of Candies
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:
- Use max function to find maximum number in list.
- If current number add extraCandies > maximum number, return True.
- 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: