Calculate Processed Work Units in Pipelined Task Queues
Dynamic ProgrammingAuto-graded · 22 test casesLast updated
Simulate task batch execution across a sequential multi-stage pipeline and count total units completed within a time limit.
You are configuring an asynchronous pipeline consisting of $K$ sequential processing nodes numbered $0$ to $K - 1$.
Each node $i$ is specified as a tuple (capacity, step_time):
capacity: The maximum number of task units the node can process simultaneously ($capacity \ge 1$).step_time: The time taken to execute one full execution cycle at this node ($step_time \ge 0$).
A sequence of task groups is pushed into the pipeline starting at time $t = 0$. You are given an array batches, where batches[j] denotes the number of units in group $j$.
The system operates under the following constraints:
- Parallel Execution Cycles: At node $i$, a group containing $B$ units is processed in consecutive cycles. Each cycle runs up to
capacityunits and takesstep_timeunits of time. The total cycles needed for group $j$ at node $i$ is $\lceil B / capacity \rceil$. If $B = 0$, processing takes $0$ time. - Barrier Synchronization: Group $j$ cannot begin processing at node $i + 1$ until all units in group $j$ have completely finished processing at node $i$.
- Sequential Node Availability: A node can handle only one task group at a time. Node $i$ cannot start group $j$ until it finishes group $j - 1$.
- Pipeline Concurrency: Different nodes can process different task groups concurrently.
- Deadline Condition: A task group counts toward the total completed units if and only if its execution through the final node ($K - 1$) finishes at or before
deadline. If a group exceeds the deadline, none of its units are counted.
Calculate and return the total number of task units that successfully finish execution by the given deadline.
Constraints
1 <= len(stages) <= 1,0001 <= stages[i][0] <= 10^6(capacity)0 <= stages[i][1] <= 10^6(step_time)0 <= len(batches) <= 10^40 <= batches[j] <= 10^90 <= deadline <= 10^18
Examples
Example 1
Input: count_completed_jobs([[2, 3], [1, 4]], [3, 2], 20)
Output: 3
Example 2
Input: count_completed_jobs([[3, 2]], [0, 5], 4)
Output: 5
Hints
Hint 1
For each batch $j$ and node $i$, determine the start time by considering two availability bounds: when node $i$ finishes batch $j - 1$, and when batch $j$ finishes node $i - 1$.
Hint 2
The total execution time for a batch $B$ with capacity $C$ and duration $D$ is $\lceil B / C \rceil \times D$. Use integer division `(B + C - 1) // C` to avoid precision issues.
Hint 3
Track state using an array of length $K$ for when each node becomes free, alongside a scalar variable for when the current batch clears its previous stage.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem models flow-shop scheduling across $K$ processing stages in FIFO order. We can track the completion timestamp of each batch at each stage using dynamic programming / state tracking.
Let $F[j][i]$ be the time at which batch $j$ finishes stage $i$. Batch $j$ at stage $i$ must wait for two dependencies:
- Node dependency: Stage $i$ must be free, which happens at time $F[j-1][i]$.
- Batch dependency: Batch $j$ must finish stage $i - 1$, which happens at time $F[j][i-1]$.
Thus, batch $j$ starts processing at stage $i$ at time: $$\text{start} = \max(F[j-1][i], F[j][i-1])$$
The processing time $P(j, i)$ required at stage $i$ for $B_j$ units is: $$P(j, i) = \left\lceil \frac{B_j}{\text{capacity}_i} \right\rceil \times \text{step_time}_i$$
Using integer ceiling division in Python: (B_j + capacity_i - 1) // capacity_i.
The finish time is $F[j][i] = \text{start} + P(j, i)$.
Space Optimization
We do not need a full 2D table of size $N \times K$:
- $F[j-1][i]$ is represented by an array
node_free_timeof size $K$. - $F[j][i-1]$ is represented by a single scalar variable
prev_node_completion.
For each batch, we iterate through all $K$ stages, updating node_free_time[i] and prev_node_completion. If prev_node_completion <= deadline after stage $K - 1$, we add the batch size to our answer.
Complexity
- Time Complexity: $\mathcal{O}(N \cdot K)$, where $N$ is the number of batches and $K$ is the number of stages. With $N \le 10^4$ and $K \le 10^3$, the maximum number of operations is $\approx 10^7$, well within execution limits.
- Space Complexity: $\mathcal{O}(K)$ extra space to store the completion time of each stage.
from typing import List, Tuple
def count_completed_jobs(stages: List[Tuple[int, int]], batches: List[int], deadline: int) -> int:
node_free_time = [0] * len(stages)
total_completed = 0
for batch_units in batches:
prev_node_completion = 0
for idx, (capacity, step_time) in enumerate(stages):
start_time = max(node_free_time[idx], prev_node_completion)
cycles = (batch_units + capacity - 1) // capacity if batch_units > 0 else 0
end_time = start_time + cycles * step_time
node_free_time[idx] = end_time
prev_node_completion = end_time
if prev_node_completion <= deadline:
total_completed += batch_units
return total_completed