Which station has the least work
Jobs are queued against stations on a line. Find the station currently carrying the fewest hours so the next job can be routed there.
- Each station is an index from 0 up to stationCount - 1.
- Sum the hours of every job queued at each in-range station.
- The answer is the index with the smallest total; ties go to the lowest index.
- Jobs pointing at an out-of-range station are ignored.
- With zero stations or no in-range jobs the answer is 0.
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.
Where you start
int leastLoadedStation(std::vector<Job> jobs, int stationCount) {
}
Worked examples
| Call | Result |
|---|---|
leastLoadedStation(std::vector<Job>{Job{0, 10}, Job{1, 5}, Job{0, 2}}, 2) | 1 |
leastLoadedStation(std::vector<Job>{Job{0, 3}, Job{1, 3}}, 2) | 0 |
leastLoadedStation(std::vector<Job>{Job{5, 10}}, 3) | 0 |
leastLoadedStation(std::vector<Job>{}, 4) | 0 |
Hint
Tally per station in an array, then walk the array keeping a running best index.
Reference solution in C++
int leastLoadedStation(std::vector<Job> jobs, int stationCount) {
if (stationCount <= 0) return 0;
std::vector<int> totals(stationCount, 0);
for (const auto& j : 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;
}