Growing Worker Pool Sized by Peak Load Readings
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Track a pool that only grows, sized to cover each load reading with fixed-capacity workers; tests ceiling division and running-maximum reasoning with large values.
Growing Worker Pool Sized by Peak Load Readings
A job-processing service runs on a pool of identical workers. Each worker can absorb at most capacity_per_worker units of load. Load is measured at regular intervals, giving a chronological list loads, where loads[i] is the total load recorded in the i-th interval.
The pool begins with exactly 1 worker. Go through the loads in order. After looking at each load value:
- If the value is at most the pool's total capacity (
workers * capacity_per_worker), leave the pool as is. - Otherwise, add the smallest number of workers that makes the total capacity at least as large as the value. Added workers are usable immediately.
Workers are never removed, so the pool size never shrinks.
Return a list sizes with the same length as loads, where sizes[i] is the number of workers in the pool after handling loads[i].
Constraints
1 <= len(loads) <= 10^51 <= capacity_per_worker <= 10^90 <= loads[i] <= 10^13(values can exceed the 32-bit integer range)- Every value in the returned list fits in a 64-bit integer.
Examples
Example 1
Input: scale_worker_pool(10, [5, 25, 18, 40, 32])
Output: [1, 3, 3, 4, 4]
Example 2
Input: scale_worker_pool(5, [0, 1, 5, 6, 10, 11])
Output: [1, 1, 1, 2, 2, 3]
Example 3
Input: scale_worker_pool(10, [100, 1, 99, 101])
Output: [10, 10, 10, 11]
Hints
Hint 1
For a single load value on its own, how many workers are needed at minimum? Express it as a division that rounds up.
Hint 2
Adding workers one at a time can take far too long when the pool needs to grow to billions of workers. Jump directly to the required size instead.
Hint 3
Because the pool never shrinks and starts at 1, each answer is a running maximum of the per-load requirements (and 1). Use integer arithmetic, (x + c - 1) // c, for the rounding up.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
For a single load value v, the fewest workers that can handle it alone is the smallest integer k with k * capacity_per_worker >= v. That is the ceiling of v / capacity_per_worker, computed with integers as (v + c - 1) // c.
Now consider the rule. If the current pool already has at least k workers, nothing changes. Otherwise the smallest pool that covers v has exactly k workers, so we grow to k. In both cases the new size is max(current, k).
Since the pool starts at 1 and never shrinks, the answer after each reading is the running maximum of 1 and all the per-reading requirements seen so far.
Why it is correct
Induct on the readings. Before any reading the size is 1, which equals max(1). Suppose the size before reading i is S, the maximum of 1 and the earlier requirements. If k_i <= S, then S * c >= v_i, so no change and the size stays max(S, k_i) = S. If k_i > S, then any pool covering v_i needs at least k_i workers and k_i suffices, so the size becomes k_i = max(S, k_i).
A load of 0 gives a requirement of 0, which never lowers the pool below 1. A load exactly equal to the capacity gives a requirement equal to the current size, so nothing is added.
Avoiding pitfalls
- Use integer ceiling division instead of floats, which lose precision for large values.
- Do not add workers in a loop; the pool can need up to 10^13 workers, which would be far too slow.
Complexity
- Time: O(n), one division and one comparison per reading.
- Space: O(1) extra beyond the O(n) output.
Reference solution
from typing import List
def scale_worker_pool(capacity_per_worker: int, loads: List[int]) -> List[int]:
workers = 1
sizes = []
for value in loads:
needed = (value + capacity_per_worker - 1) // capacity_per_worker
if needed > workers:
workers = needed
sizes.append(workers)
return sizes