Problems › TypeScript › machines
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
Where you start
function runHours(operations: Operation[]): number {
}
Worked examples
| Call | Result |
|---|---|
runHours([{"start":0,"end":60},{"start":600,"end":900}]) | 6 |
runHours([{"start":540,"end":600}]) | 1 |
runHours([]) | 0 |
runHours([{"start":0,"end":30}]) | 0 |
Hint
Sum the minute spans, then integer-divide the total by sixty.
Reference solution in TypeScript
function runHours(operations: Operation[]): number {
let total = 0;
for (const o of operations) total += o.end - o.start;
return Math.floor(total / 60);
}