Skip to content
RED SAGSkills
easy15 ptsqueuesimulation

Print Queue Finish Time

People stand in a queue at a shared printer; person i needs pages[i] pages. Each second, the person at the front prints exactly one page. If they still have pages left they go to the back of the queue, otherwise they leave.

Return the second (counting from 1) at which person k prints their last page.

Example 1

Input: pages = [2, 3, 2], k = 2
Output: 6

Example 2

Input: pages = [5, 1, 1, 1], k = 0
Output: 8
Explanation: Person 0 prints at seconds 1, 5, 6, 7 and 8.

Constraints

  • 1 <= pages.length <= 10^5
  • 1 <= pages[i] <= 100
  • 0 <= k < pages.length
Hint 1

You can simulate with a queue, but there is a direct formula.

Hint 2

Everyone at or before k prints at most pages[k] pages before k finishes; everyone after prints at most pages[k] - 1.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer, DevOps Engineer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.