FunctionRacerProblemsRace

Ticket Line Finish Order

EasyStacks and queuesPython, JavaScript, TypeScript, Java, C++

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

tickets [2,3,2]
returns [0,2,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

tickets [1,1,1]
returns [0,1,2]

Everyone wants one ticket, so they leave in line order.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Stacks and queues problems