Count Completed Jobs in a Serial Multi-Worker Pipeline
An automation pipeline contains stages that execute in order. Each stage is represented as [number_of_jobs, job_time, number_of_workers]. Workers within one stage process jobs in parallel, but each worker handles at most one job at a time and a job cannot be split. The next stage begins only after every job in the current stage is complete. Given a total time limit, return how many jobs finish completely.
Function Signature
def calculate_jobs_with_workers(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 every job in that stage.
-
pipeline[i][2]
is the number of workers available only to that stage.
-
All workers in a stage are available when the stage starts.
Output
Return the total number of jobs completed by time_limit. Jobs in progress at the limit do not count.
Constraints
-
0 <= len(pipeline) <= 100_000
-
Every stage has exactly three integers.
-
0 <= number_of_jobs <= 10^9
-
1 <= job_time, number_of_workers <= 10^9
-
0 <= time_limit <= 9_000_000_000_000_000
-
Across the complete pipeline,
sum(ceil(number_of_jobs / number_of_workers) * job_time) <= 9_000_000_000_000_000
.
-
These limits keep every batch count, 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, 2], [10, 1, 1]]
time_limit = 14
output = 12
Two workers finish the first four jobs in 6 time units. During the remaining 8 time units, the single worker in stage two finishes eight jobs.
pipeline = [[5, 4, 2], [3, 1, 3]]
time_limit = 9
output = 4
Four jobs finish in two complete four-unit batches. The fifth job is incomplete at the limit, so the next stage never begins.