Total hours a machine ran
A machine records operations as start and end minutes past midnight. Convert the whole running total into whole hours.
- Each operation adds end minus start minutes.
- Total is reported in whole hours, truncated down.
- Operations never cross midnight and every end is after its start.
runHours(operations: list<Operation>) → 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 runHours(std::vector<Operation> operations) {
}
Worked examples
| Call | Result |
|---|---|
runHours(std::vector<Operation>{Operation{0, 60}, Operation{600, 900}}) | 6 |
runHours(std::vector<Operation>{Operation{540, 600}}) | 1 |
runHours(std::vector<Operation>{}) | 0 |
runHours(std::vector<Operation>{Operation{0, 30}}) | 0 |
Hint
Sum the minute spans, then integer-divide the total by sixty.
Reference solution in C++
int runHours(std::vector<Operation> operations) {
int total = 0;
for (const auto& o : operations) total += o.end - o.start;
return total / 60;
}