Finish Times for a Single Worker with a Limited Waiting Line
Stacks & QueuesAuto-graded · 20 test casesLast updated
Simulate a one-worker FIFO queue with a capped waiting line and report each job's finish time or -1 if it was rejected, using a deque of finish times.
Job Processing with a Capped Waiting Line
A service has exactly one worker. Jobs show up over time, and the worker handles them one at a time. Once the worker begins a job, it runs until the job is done and cannot be interrupted.
You are given an integer waiting_capacity, the largest number of jobs that may be waiting at the same moment. The job the worker is currently running does not count toward this limit. You are also given a list jobs, where each entry is a pair [arrival, length]:
arrival: the time at which the job reaches the service.length: how long the worker needs to run the job.
Rules
- The worker runs at most one job at a time, and a job that has started runs to completion.
- When a job arrives at time
arrival:- Every job whose finish time is less than or equal to
arrivalhas already left the system. - If the worker is idle, the new job starts right away.
- If the worker is busy and the number of jobs currently waiting is strictly less than
waiting_capacity, the new job joins the waiting line. - Otherwise (the worker is busy and the waiting line already holds
waiting_capacityjobs), the new job is rejected immediately and never runs.
- Every job whose finish time is less than or equal to
- Waiting jobs are served in first-come, first-served order. Jobs with the same arrival time are handled in the order they appear in
jobs.
Task
Return a list of integers with one entry per job, in input order. Entry i is the time at which job i finishes running, or -1 if job i was rejected.
If jobs is empty, return an empty list.
Constraints
0 <= waiting_capacity <= 10^50 <= len(jobs) <= 10^5(the list may be empty)0 <= arrival <= 10^91 <= length <= 10^9- Jobs are given in non-decreasing order of
arrival.
Examples
Example 1
Input: job_finish_times(1, [[1, 5], [2, 4], [3, 3], [7, 2]])
Output: [6, 10, -1, 12]
Example 2
Input: job_finish_times(0, [[0, 3], [2, 2], [3, 1]])
Output: [3, -1, 4]
Hints
Hint 1
You do not need to simulate every tick of a clock. Once a job is accepted, its finish time is fixed, because later arrivals cannot change the FCFS order.
Hint 2
Keep the finish times of all accepted jobs that are still in the system (the running one plus the waiting ones) in a deque. Before handling an arrival, pop from the front every finish time that is <= the arrival time.
Hint 3
After removing finished jobs, if the deque is empty the worker is idle. Otherwise the front job is running and the waiting count is len(deque) - 1. A new accepted job starts at the last finish time in the deque (or at its arrival if the deque is empty).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
There is no need to step a clock forward. Because service is first-come, first-served and non-preemptive, the moment a job is accepted its finish time is determined: it starts when the previously accepted job finishes (or at its own arrival if the system is empty) and runs for its length.
So we only track the finish times of accepted jobs that are still in the system, that is, the running job plus the waiting jobs, in a deque.
For each arriving job at time a:
- Evict. Pop from the front while the front finish time is
<= a. A job finishing exactly atahas already freed its place. - Admit or reject.
- If the deque is empty, the worker is idle, so the job starts at
aand finishes ata + length. - Otherwise the front job is running and
len(deque) - 1jobs are waiting. If that count is strictly less thanwaiting_capacity, accept the job: it starts at the last finish time in the deque and finisheslengthlater. - Otherwise record
-1.
- If the deque is empty, the worker is idle, so the job starts at
- Append the finish time of an accepted job to the deque and to the result.
Why it is correct
- Finish times in the deque are strictly increasing, since each accepted job starts no earlier than its predecessor's finish and lengths are at least 1. So eviction from the front removes exactly the jobs that are done.
- After eviction, the front job is the one running. Every job accepted before it has finished by
a, and it arrived no later thana, so its start is at or beforeawhile its finish is aftera. All later entries have not started yet, so they are waiting. This gives "1 running + (len - 1) waiting", which justifies the capacity check. - A later arrival can never alter an already accepted job's schedule under FCFS without preemption, so computing its finish time on arrival is valid.
- Jobs with equal arrival times are processed in input order, each seeing the state left by the previous one.
Complexity
- Time: O(n), since each job is appended once and popped at most once.
- Space: O(n) for the output, and the deque holds at most
waiting_capacity + 1entries.
Reference solution
from collections import deque
from typing import List
def job_finish_times(waiting_capacity: int, jobs: List[List[int]]) -> List[int]:
in_system = deque() # finish times of the running job and waiting jobs, increasing
results = []
for arrival, length in jobs:
# Drop jobs that are done by the time this one arrives.
while in_system and in_system[0] <= arrival:
in_system.popleft()
if not in_system:
finish = arrival + length
in_system.append(finish)
results.append(finish)
elif len(in_system) - 1 < waiting_capacity:
finish = in_system[-1] + length
in_system.append(finish)
results.append(finish)
else:
results.append(-1)
return results