Drill

ProblemsC# › logistics

Split a load into the fewest parcels

easylogisticsMathC#

A warehouse has to ship a totalWeight in parcel boxes that hold at most maxPerBox each. Find how many parcels are needed.

ParcelCount(totalWeight: int, maxPerBox: int) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int ParcelCount(int totalWeight, int maxPerBox) {
    
}

Worked examples

CallResult
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 C#
public int ParcelCount(int totalWeight, int maxPerBox) {
    if (totalWeight <= 0 || maxPerBox <= 0) return 0;
    return (totalWeight + maxPerBox - 1) / maxPerBox;
}

The same problem in another language

More logistics problems in C#