Problems › TypeScript › logistics
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.
parcelCount(totalWeight: int, maxPerBox: int) → int
Where you start
function parcelCount(totalWeight: number, maxPerBox: number): number {
}
Worked examples
| Call | Result |
|---|---|
parcelCount(100, 20) | 5 |
parcelCount(101, 20) | 6 |
parcelCount(0, 20) | 0 |
parcelCount(50, 0) | 0 |
Hint
Divide and round up; watch the division-by-zero case first.
Reference solution in TypeScript
function parcelCount(totalWeight: number, maxPerBox: number): number {
if (totalWeight <= 0 || maxPerBox <= 0) return 0;
return Math.floor((totalWeight + maxPerBox - 1) / maxPerBox);
}