Minimum Bandwidth to Stream All Files Within Deadline
Heaps, Sorting & SearchAuto-graded · 17 test casesLast updated
Tests binary search on the answer to find the minimum integer bandwidth that allows streaming a list of files (each with a size and deadline) without missing any deadline using a greedy scheduler.
Minimum Bandwidth to Stream All Files Within Deadline
You are building a media streaming system. There are n files to stream. Each file i has:
- a size
sizes[i](in megabytes, MB), which is the total data that must be delivered, and - a deadline
deadlines[i](in seconds from now), by which the file must be fully delivered.
The system streams at a constant bandwidth of B MB/s, and can only stream one file at a time. You must schedule all files in some order, starting at time 0. File i is delivered at the moment its last byte is sent, which equals the sum of the sizes of all files scheduled before it plus its own size, divided by B.
A schedule is feasible for bandwidth B if there exists an ordering of the files such that every file is fully delivered by its deadline.
Return the minimum integer bandwidth B ≥ 1 (in MB/s) such that a feasible schedule exists.
Scheduling Rule
For a given bandwidth B, to check feasibility use the Earliest Deadline First (EDF) policy: sort files by deadline in ascending order, breaking ties by index (smaller index first). Greedily deliver them in that order. A schedule is feasible if and only if no file misses its deadline under EDF.
Constraints
1 ≤ n ≤ 20001 ≤ sizes[i] ≤ 10^61 ≤ deadlines[i] ≤ 10^9- The answer always exists and is at most
10^9. - All values are integers.
Examples
Example 1
Input: min_bandwidth([10, 20, 30], [5, 4, 9])
Output: 7
Example 2
Input: min_bandwidth([100], [10])
Output: 10
Example 3
Input: min_bandwidth([5, 5, 5, 5], [4, 8, 12, 16])
Output: 2
Hints
Hint 1
The feasibility check for a fixed bandwidth B is straightforward: sort files by (deadline, index) and simulate delivery times. Think about what property feasibility has as B increases.
Hint 2
If bandwidth B is feasible, then any bandwidth B' > B is also feasible. This monotonicity means you can binary search on B.
Hint 3
For the binary search bounds: the minimum possible answer is 1, and an upper bound is ceil(max(sizes[i]) / min(deadlines[i])) * n or simply sum(sizes) — think about a safe upper bound that is guaranteed to be feasible.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight: Monotonicity
If bandwidth B is feasible (all files can be delivered on time under EDF), then any bandwidth B' > B is also feasible, because every delivery time strictly decreases. This monotone property makes binary search on B correct.
Feasibility Check (EDF)
For a fixed integer bandwidth B, we check feasibility using Earliest Deadline First:
- Sort files by
(deadline, index)ascending. - Simulate: track cumulative bytes delivered so far (
time_num). After scheduling filei, cumulative bytes =time_num + sizes[i]. The delivery time istime_num / Bseconds. - To avoid floating point, the condition "delivered on time" is:
time_num ≤ deadlines[i] * B(all integers).
EDF is optimal for single-machine scheduling with deadlines on a preemptable or unit machine; here the non-preemptive case with arbitrary sizes is trickier in general, but the problem defines EDF as the required check rule — so we apply it exactly as specified.
Binary Search Bounds
- Lower bound:
1(minimum valid bandwidth). - Upper bound:
sum(sizes)MB/s — at this bandwidth every file is delivered within 1 second of the start, so all deadlines (≥ 1) are trivially met.
The search space has at most sum(sizes) ≤ 2000 × 10^6 = 2 × 10^9 values, so binary search takes at most ~31 iterations.
Complexity
- Feasibility check: O(n log n) for sorting + O(n) simulation = O(n log n).
- Binary search: O(log(sum(sizes))) ≈ O(31) iterations.
- Overall: O(n log n · log(sum(sizes))).
With n ≤ 2000 this is very fast in practice.
from typing import List
def min_bandwidth(sizes: List[int], deadlines: List[int]) -> int:
n = len(sizes)
def is_feasible(B: int) -> bool:
# EDF: sort by (deadline, original index)
order = sorted(range(n), key=lambda i: (deadlines[i], i))
time_num = 0 # cumulative bytes sent; actual time = time_num / B
for i in order:
time_num += sizes[i]
# delivered at time_num / B <= deadlines[i] iff time_num <= deadlines[i] * B
if time_num > deadlines[i] * B:
return False
return True
lo, hi = 1, sum(sizes)
while lo < hi:
mid = (lo + hi) // 2
if is_feasible(mid):
hi = mid
else:
lo = mid + 1
return lo