Seconds Until a Chosen Ticket Buyer Finishes. People initially stand in the order of tickets. Each second the front person buys one ticket, then immediately rejoins the back if more are needed, otherwise leaves. tickets[i] is person i's initial demand. Return the time when the person initially at index k buys their final ticket.
### Problem
People initially line up in array order, and person `i` wants to buy `tickets[i]` tickets. Each purchase takes exactly one second and buys one ticket. After a purchase, a person who still needs tickets immediately joins the back of the line; otherwise that person leaves. Return the number of seconds until the person who started at zero-based position `k` finishes buying all their tickets.
```cpp
class Solution {
public:
int timeRequiredToBuy(vector<int>& tickets, int k);
};
```
### Examples
**Example 1:**
```text
tickets = [1,2,1], k = 1
Output: 4
Explanation: Buyers 0,1,2,1 purchase in that order.
```
**Example 2:**
```text
tickets = [3,2], k = 0
Output: 5
Explanation: Buyers 0,1,0,1,0 purchase before the target finishes.
```
### Constraints
- `1 <= tickets.length <= 100`; `1 <= tickets[i] <= 100`.
- `0 <= k < tickets.length`.
### Clarifying Questions
Return time through and including the target's last purchase. Initial position k identifies a person, not a changing queue index.
```hint Inspect the structure
How many rounds can a person after k complete before the target's last purchase?
```
### Reasoning checkpoints
- Identify the original target person throughout changes to the queue.
- Include the target's final purchase and stop counting immediately afterward.
- Handle people who leave early and targets at either end of the initial line.
### Optional extensions
How would you recover the individual purchase order if it were also requested?
Overview: Seconds Until a Chosen Ticket Buyer Finishes. People initially stand in the order of tickets. Each second the front person buys one ticket, then immediately rejoins the back if more are needed, otherwise leaves. tickets[i] is person i's initial demand. Return the time when the person initially at index k buys their final ticket.
People initially line up in array order, and person i wants to buy tickets[i] tickets. Each purchase takes exactly one second and buys one ticket. After a purchase, a person who still needs tickets immediately joins the back of the line; otherwise that person leaves. Return the number of seconds until the person who started at zero-based position k finishes buying all their tickets.
class Solution {
public:
int timeRequiredToBuy(vector<int>& tickets, int k);
};
Examples
Example 1:
tickets = [1,2,1], k = 1
Output: 4
Explanation: Buyers 0,1,2,1 purchase in that order.
Example 2:
tickets = [3,2], k = 0
Output: 5
Explanation: Buyers 0,1,0,1,0 purchase before the target finishes.