Ticket Line Finish Order
People stand in a line at a ticket window. The person at position i of the starting line wants tickets[i] tickets. Each turn, the window sells one ticket to the person at the front of the line. If that person still wants more tickets, they walk to the back of the line; otherwise they leave.
Return the people, identified by their position in the starting line, in the order in which they leave. Return an empty list if the line is empty.
Example 1
Person 0 buys, goes to the back; person 1 buys, goes back; person 2 buys, goes back. Then person 0 buys their second ticket and leaves, person 1 goes back again, and person 2 leaves before person 1 finishes.
Example 2
Everyone wants one ticket, so they leave in line order.
Constraints
0 <= len(tickets) <= 1001 <= tickets[i] <= 20
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Stacks and queues problems
- Typing With Home and EndEasy
- Where cd LandsEasy
- Balanced Outside QuotesMedium
- Browser Back and ForwardMedium
- Convoys at the GateMedium
- Daily TemperaturesMedium
- Decode StringMedium
- Flip and Double BracketsMedium