Drill

ProblemsC# › machines

Which station has the least work

hardmachinesHash mapsArraysSortingC#

Jobs are queued against stations on a line. Find the station currently carrying the fewest hours so the next job can be routed there.

LeastLoadedStation(jobs: list<Job>, stationCount: 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 LeastLoadedStation(List<Job> jobs, int stationCount) {
    
}

Worked examples

CallResult
LeastLoadedStation(new List<Job> { new Job(0, 10), new Job(1, 5), new Job(0, 2) }, 2)1
LeastLoadedStation(new List<Job> { new Job(0, 3), new Job(1, 3) }, 2)0
LeastLoadedStation(new List<Job> { new Job(5, 10) }, 3)0
LeastLoadedStation(new List<Job> { }, 4)0

Hint

Tally per station in an array, then walk the array keeping a running best index.

Reference solution in C#
public int LeastLoadedStation(List<Job> jobs, int stationCount) {
    if (stationCount <= 0) return 0;
    int[] totals = new int[stationCount];
    foreach (var j in jobs) {
        if (j.Station < 0 || j.Station >= stationCount) continue;
        totals[j.Station] += j.Hours;
    }
    int best = 0;
    for (int i = 1; i < stationCount; i++) if (totals[i] < totals[best]) best = i;
    return best;
}

The same problem in another language

More machines problems in C#