tutorialcup
Distribute Candies to People Leetcode Solution
Problem Statement
In this problem, we are given two numbers candies and num_people. The first number candies is the number of candies we have. num_people shows the number of person in which we have to distribute the candies.
The rule of candies distribution is:
We start from the leftmost person give him 1 candy then we give 2 candies to 2nd person, 3 candies to 3rd person till n candies to nth person. After that we again start from leftmost person giving him n+1 candies then n+2, n+3. This cyclic distribution continues till we run out of candies i.e. when our remaining candies will be less than current requirement, we will give the remaining candies to current person and then we stop.
Example
candies = 7, num_people = 4
Explanation:
On the first turn, ans += 1, and the array is .
On the second turn, ans += 2, and the array is .
Third turn, ans += 3, and the array is .
Fourth turn, ans += 1 (because there is only one candy left), and the final array is .
candies = 10, num_people = 3
Explanation:
On the first turn, ans += 1, and the array is .
On the second turn, ans += 2, and the array is .
Third turn, ans += 3, and the array is .
Fourth turn, ans += 4, and the final array is .
Approach 1 (Brute Force)
The simplest approach is to give l candy to first person and give 2 to 2nd person and so on move to last person. Then again start from first person giving him candies accordingly.
This
www.tutorialcup.com/leetcode-solutions/distribute-candies...
Distribute Candies to People Leetcode Solution
Problem Statement
In this problem, we are given two numbers candies and num_people. The first number candies is the number of candies we have. num_people shows the number of person in which we have to distribute the candies.
The rule of candies distribution is:
We start from the leftmost person give him 1 candy then we give 2 candies to 2nd person, 3 candies to 3rd person till n candies to nth person. After that we again start from leftmost person giving him n+1 candies then n+2, n+3. This cyclic distribution continues till we run out of candies i.e. when our remaining candies will be less than current requirement, we will give the remaining candies to current person and then we stop.
Example
candies = 7, num_people = 4
Explanation:
On the first turn, ans += 1, and the array is .
On the second turn, ans += 2, and the array is .
Third turn, ans += 3, and the array is .
Fourth turn, ans += 1 (because there is only one candy left), and the final array is .
candies = 10, num_people = 3
Explanation:
On the first turn, ans += 1, and the array is .
On the second turn, ans += 2, and the array is .
Third turn, ans += 3, and the array is .
Fourth turn, ans += 4, and the final array is .
Approach 1 (Brute Force)
The simplest approach is to give l candy to first person and give 2 to 2nd person and so on move to last person. Then again start from first person giving him candies accordingly.
This
www.tutorialcup.com/leetcode-solutions/distribute-candies...