Similar Problems
Similar Problems not available
Distribute Candies To People - Leetcode Solution
Companies:
LeetCode: Distribute Candies To People Leetcode Solution
Difficulty: Easy
Topics: math simulation
Problem Statement:
We have n people and m candies. We must distribute those candies equally among all the people and accept the remainder as distributed. That is, if you give each person the same number of candies, there will be some number of candies left over that are not distributed. Please return a list which contains the number of candies each person receives, along with the number of candies left over.
Solution:
The given problem requires us to distribute the candies among the people equally, and as a result, we can assume that each person should receive the same number of candies.
First, we need to find out how many candies can be distributed equally by dividing the total number of candies by the number of people.
Then, we should check what is left after distributing the candies equally among all the people. We can find this out by calculating the remainder of the total candies divided by the number of people.
Finally, we should distribute the leftover candies among the people one by one until we run out of candies to distribute.
Therefore the implementation of the above logic, please look at the code snippet given below:
def distributeCandies(candies: int, num_people: int) -> List[int]:
# Calculate the number of candies that can be distributed evenly among all people
n = num_people
even_distribution = int((2 * candies + 0.25)**0.5 - 0.5)
even_distribution = min(even_distribution, n * (n + 1) // 2)
# Calculate the number of candies left over after even distribution
candies_left = candies - (even_distribution * (even_distribution + 1) // 2)
# Distribute candies evenly among all people
distribution = [0] * n
for i in range(n):
if i < even_distribution % n:
distribution[i] = (even_distribution // n + 1) * (i + 1) + (even_distribution // n) * n * (n + 1) // 2
else:
distribution[i] = (even_distribution // n) * (i + 1) + (even_distribution // n) * n * (n + 1) // 2
# Distribute the remaining candies one by one
idx = 0
while candies_left > 0:
extra_candies = min(candies_left, even_distribution + 1)
distribution[idx] += extra_candies
candies_left -= extra_candies
idx = (idx + 1) % n
return distribution
In the above implementation, we first calculate the number of candies that can be distributed equally among all the people. We then remove the distributed candies from the total number of candies to get the number of leftover candies.
We then distribute the evenly distributed candies among all people, considering the number of people and the remainder of candies while distributing them. The remaining candies are then distributed one by one among the people until there are no more candies left to distribute.
Finally, we return the list containing the number of candies each person receives.
The time complexity of the above implementation is O(n), where n is the number of people, and the space complexity is O(n), where n is the number of people.
Distribute Candies To People Solution Code
1