Count Completed Jobs in a Serial Single-Worker Pipeline
An automation pipeline contains stages that execute in order. Each stage is represented as [number_of_jobs, job_time]. A stage has one worker, its jobs run one at a time, and the next stage cannot begin until every job in the current stage is complete. Given a total time limit, return how many jobs across the pipeline finish completely within that time.
Function Signature
def calculate_jobs_completed(pipeline: list[list[int]], time_limit: int) -> int:
Inputs
-
pipeline[i][0]
is the number of jobs in stage
i
.
-
pipeline[i][1]
is the integer duration of each job in that stage.
-
time_limit
is the total time available from the start of stage 0.
Output
Return the total number of fully completed jobs. A job still running when the time limit expires does not count.
Constraints
-
0 <= len(pipeline) <= 100_000
-
Every stage has exactly two integers.
-
0 <= number_of_jobs <= 10^9
-
1 <= job_time <= 10^9
-
0 <= time_limit <= 9_000_000_000_000_000
-
Across the complete pipeline,
sum(number_of_jobs * job_time) <= 9_000_000_000_000_000
.
-
These limits keep every stage duration, cumulative duration, deadline, and returned job count within the exact integer range shared by JavaScript
Number
and signed 64-bit Java/C++ integers.
-
The input collection must not be mutated.
Examples
pipeline = [[4, 3], [10, 1]]
time_limit = 14
output = 6
The first stage completes four jobs in 12 time units. Two time units remain, so two jobs complete in the second stage.
pipeline = [[3, 5], [2, 1]]
time_limit = 12
output = 2
Only two jobs in the first stage finish; the second stage cannot start.