Distribute Candies LeetCode
In the world of sharing and fairness, the distribute candies challenge of LeetCode is really interesting. Itβs about figuring out how to give a limited number of candies to a group of people so that everyone gets a fair amount, following certain rules. This problem is like real-life situations where we have to share resources fairly, like giving money to different departments of an organization or distributing supplies to people affected by some disaster.
Problem statement
We are given
Here is the list of requirements to remember:
- We have a total of candies.
- Candies are always even.
- The type of each candy is stored in the list.
- We can give only half of our candies.
- The given number of candies must contain the maximum types of candies.
Our task is to determine the maximum number of different candies that we can share. The result would be a number of different types of candies. Letβs try to map our problem into a visual for better understanding.
Educative-99Β helps you solve complex coding problems like distribute candies by teaching you to recognize patterns and apply the right algorithms.
Algorithm
There are
Letβs write down the algorithm steps:
Find the total number of candies to share (half of the total candies).
Find the total number of types of candies (unique candies).
If the types of candies are less than the total number of candies to be shared, return the total types of candies.
Otherwise, return half of the total number of candies.
Knowledge test
Now you have understood the problem and are capable of finding the maximum types of candies to be shared.
According to the above algorithm, how many types of candies can we share from the following collection of candies?
π¬, π, π«, π‘, πͺ, π°, π¬, π¬, π, π«
4
5
6
Coding example
Letβs implement the above algorithm below:
def distributeCandies(candyType: list[int]) -> int:max_candies = len(candyType)//2 # finding the total number of candies to be sharedunique_candies = len(set(candyType)) # finding the total number of types of candies# checking if the unique_candies are less than the maximum candies to be sharedif unique_candies < max_candies:return unique_candies # return the unique_candiesreturn max_candies # otherwise return the max_candies to be shared# ---------------------------------------------------------------------candySet1 = [1, 1, 2, 2, 3, 3] # candy set 1print("For the candy set:", candySet1, end="")print(", we can share", distributeCandies(candySet1), "type(s) of candies!")candySet2 = [1, 1, 2, 3] # candy set 2print("For the candy set:", candySet2, end="")print(", we can share", distributeCandies(candySet2), "type(s) of candies!")candySet3 = [6, 6, 6, 6] # candy set 3print("For the candy set:", candySet3, end="")print(", we can share", distributeCandies(candySet3), "type(s) of candies!")candySet4 = ['π¬', 'π', 'π«', 'π‘', 'πͺ', 'π°', 'π¬', 'π¬', 'π', 'π«'] # candy set 4print("For the candy set:", candySet4, end="")print(", we can share", distributeCandies(candySet4), "type(s) of candies!")
Time complexity
In the above implementation, we use:
Finding the length takes the constant time:
. Finding unique elements takes the linear time:
. Condition check takes the constant time:
.
By the above distribution, the time complexity is linear:
Space complexity
In the above implementation, we use:
Two variables to store two integer values.
The set of unique candies to store at most
elements.
By the above distribution, the space complexity is linear:
Free Resources