Ctrl K

One Printer, Shortest Job Next

Medium
An office has one printer. There are n print jobs numbered 0 to n - 1: jobs[i] = [appear, length] means job i shows up at time appear and takes exactly length units of time to print. The printer behaves like this: - if it is free and nothing is waiting, it stays idle until the next job shows up; - if it is free and jobs are waiting, it picks the shortest one; on a tie in length, the one with the smaller index; - a job, once started, prints to the end without interruption; - after finishing a job, the printer can start the next one at that same moment (jobs that show up exactly then are also in the running). Return the job indices in the order the printer prints them.
Example 1
jobs=[[2, 5], [3, 1], [4, 2], [4, 1]] → [0, 1, 3, 2]

At time 2 only job 0 is waiting; it prints until 7. By then jobs 1, 2 and 3 are waiting; 1 and 3 are shortest (1 each), so 1 goes first by its smaller index, then 3, then 2.

Example 2
jobs=[[1, 2], [10, 3], [10, 1]] → [0, 2, 1]

After job 0 the printer sits idle until time 10, then takes the shorter job 2, then job 1.

Constraints
  • 1 ≤ n = len(jobs) ≤ 10⁵
  • 1 ≤ appear, length ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsK-th Highest Score MediumStudios for Recording Sessions MediumMost Common Values MediumK-th Smallest in a Sorted Grid MediumJobs With a Cooldown Medium
loading Python…
Input
jobs=[[2, 5], [3, 1], [4, 2], [4, 1]]
Expected
[0, 1, 3, 2]

Run checks the first 3 tests in your browser. Submit checks all 109 tests on the server, hidden ones included.