Split a load into the fewest parcels
A warehouse has to ship a totalWeight in parcel boxes that hold at most maxPerBox each. Find how many parcels are needed.
- Every parcel except possibly the last carries the full maxPerBox.
- A maxPerBox of zero or less is nonsense: return 0.
- A totalWeight of zero ships zero parcels.
parcel_count(total_weight: int, max_per_box: int) → int
Where you start
def parcel_count(total_weight: int, max_per_box: int) -> int:
Worked examples
| Call | Result |
|---|---|
parcel_count(100, 20) | 5 |
parcel_count(101, 20) | 6 |
parcel_count(0, 20) | 0 |
parcel_count(50, 0) | 0 |
Hint
Divide and round up; watch the division-by-zero case first.
Reference solution in Python
def parcel_count(total_weight: int, max_per_box: int) -> int:
if total_weight <= 0 or max_per_box <= 0:
return 0
return (total_weight + max_per_box - 1) // max_per_box