FunctionRacerProblemsRace

Printer Finish Times

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

A shared printer works through a queue of jobs. Job i has pages[i] pages, and the jobs are queued in index order at the start. The printer repeatedly takes the job at the front of the queue and prints at most limit of its pages, one page per second. If the job still has pages left after that, it goes to the back of the queue; otherwise it is finished.

Return a list where item i is the second at which job i finishes. Time starts at 0, so a 3-page job printed first finishes at second 3.

Example 1

pages [3,5,2]
limit 2
returns [7,10,6]

Job 0 prints 2 pages (second 2) and requeues with 1 page; job 1 prints 2 (second 4) and requeues with 3; job 2 prints 2 and finishes at second 6; job 0 finishes at 7; job 1 prints 2 more (second 9) and its last page at second 10.

Example 2

pages [4,1]
limit 10
returns [4,5]

The limit is bigger than both jobs, so they print whole: job 0 finishes at 4 and job 1 at 5.

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