4130. Time Needed to Receive Candy
DifficultyMedium
Description
You are given two integers, numAdults and numChildren.
A candy store has two queues. The adults' queue holds numAdults adults, while the children's queue holds numChildren children followed by another child named Bob, for a total of numChildren + 1 children.
Every 5 minutes, starting at time 0, the store opens a bag of 12 candies and immediately hands them out, one candy per person, to the people at the front of the queues.
The candies in each bag are split as follows:
- 8 candies go to the children's queue and 4 go to the adults' queue.
- If either queue has fewer people than its share, its unused candies go to the other queue.
Each person leaves the queue after receiving a candy. Any candies left after both queues are empty remain unused.
Return the time, in minutes, at which Bob receives his candy.
Example 1:
Input: numAdults = 9, numChildren = 15
Output: 5
Explanation:
- At
time = 0, 4 adults and 8 children receive candies from the first bag. - After the distribution, 5 adults remain in the adults' queue, and 7 children remain ahead of Bob, making him the 8th child in the children's queue.
- At
time = 5, the second bag is opened, and Bob receives a candy. Therefore, the answer is 5.
Example 2:
Input: numAdults = 6, numChildren = 16
Output: 5
Explanation:
- At
time = 0, 4 adults and 8 children receive candies from the first bag. - After the distribution, 2 adults remain in the adults' queue, and 8 children remain ahead of Bob, making him the 9th child in the children's queue.
- At
time = 5, the second bag is opened. The remaining 2 adults receive candies, leaving 10 candies for the children's queue. Since Bob is the 9th child in the queue, he receives a candy. Therefore, the answer is 5.
Example 3:
Input: numAdults = 7, numChildren = 3
Output: 0
Explanation:
- At
time = 0, the first bag is opened. There are 7 adults and 4 children (including Bob), for a total of 11 people. - The children's queue uses 4 of its 8 candies. Its 4 unused candies go to the adults' queue, giving it 8 candies, enough for all 7 adults.
- Everyone receives a candy, including Bob. Therefore, the answer is 0.
Constraints:
0 <= numAdults, numChildren <= 100
Solutions
Solution 1: Mathematics
Thinking
With at most \(100\) people, simulating both queues bag by bag is fast enough. The extra bookkeeping is the transfer of unused candies when fewer than \(4\) adults or fewer than \(8\) children are waiting.
Let \(A=\textit{numAdults}\), and let \(C=\textit{numChildren}+1\) be the children including Bob. Before Bob is served, children take \(8\) candies per bag and take more only when an adult slot is empty. Over the first \(t\) bags, children receive at most \(8t\) candies while any adult is still waiting, and at most \(12t-A\) candies once all \(A\) adults have been served. Reaching \(C\) candies therefore needs \(t\ge\lceil C/8\rceil\) while adults remain, and \(t\ge\lceil (A+C)/12\rceil\) after every adult has been served.
A smaller \(t\) fails in both cases. Children are always offered \(8\) candies first, and no candy is wasted before Bob, so the bag index cannot exceed those bounds either. The index is their minimum. Bag \(1\) opens at time \(0\), which is \(5(t-1)\) minutes.
Let \(A=\textit{numAdults}\) and \(C=\textit{numChildren}+1\). Bob is the \(C\)-th child.
Each bag gives \(8\) candies to the children and \(4\) to the adults, and unused candies move to the other queue. The bounds above put Bob in bag
counting from \(1\). The first bag opens at time \(0\), and later bags are \(5\) minutes apart, so the answer is
Integer division writes the two ceilings as \((\textit{numChildren}+8)/8\) and \((\textit{numAdults}+\textit{numChildren}+12)/12\).
The time complexity is \(O(1)\), and the space complexity is \(O(1)\).
1 2 3 | |
1 2 3 4 5 | |
1 2 3 4 5 6 | |
1 2 3 | |
1 2 3 4 5 6 7 8 9 10 | |