Skip to content

Commit ff4820c

Browse files
committed
Adding EfficientSolution - Problem - 1431 - Kids With Greatest Candies
1 parent 9f7be86 commit ff4820c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 1431
6+
## Problem Name: Kids With the Greatest Number of Candies
7+
##===================================
8+
#
9+
#Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has.
10+
#
11+
#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.
12+
#
13+
#Example 1:
14+
#
15+
#Input: candies = [2,3,5,1,3], extraCandies = 3
16+
#Output: [true,true,true,false,true]
17+
#Explanation:
18+
#Kid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies --- the greatest number of candies among the kids.
19+
#Kid 2 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.
20+
#Kid 3 has 5 candies and this is already the greatest number of candies among the kids.
21+
#Kid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies.
22+
#Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.
23+
#Example 2:
24+
#
25+
#Input: candies = [4,2,1,1,2], extraCandies = 1
26+
#Output: [true,false,false,false,false]
27+
#Explanation: There is only 1 extra candy, therefore only kid 1 will have the greatest number of candies among the kids regardless of who takes the extra candy.
28+
#Example 3:
29+
#
30+
#Input: candies = [12,1,12], extraCandies = 10
31+
#Output: [true,false,true]
32+
class Solution:
33+
def kidsWithCandies(self, candies, extraCandies):
34+
tmpMax = max(candies) #Initialize tmpMax that finds the max number of candies
35+
tmp = [0]*len(candies) #Initialize tmp list which has same length as candies having zero value
36+
true, false = True, False #Initialize true and false variables
37+
for i in range(len(candies)): #Loop through candies
38+
if candies[i] + extraCandies >= tmpMax: #Condition-check: If candies[i] and extraCandies sum has more than or same as tmpMax
39+
tmp[i] = true #Then we update our tmp list by adding value true
40+
else: #Condition-check: Else
41+
tmp[i] = false #Otherwise update tmp list by adding value false
42+
return tmp #At the end we return tmp list

0 commit comments

Comments
 (0)