Problems › JavaScript › machines
Total minutes the line was down
A shift log stores each stop as a block of start and end minutes. Sum the length of every block to get total downtime.
- A block contributes its end minus start.
- Block never overlap and every end is after its start.
downtimeTotal(blocks: list<Block>) → int
Where you start
function downtimeTotal(blocks) {
}
Worked examples
| Call | Result |
|---|---|
downtimeTotal([{"start":0,"end":60},{"start":120,"end":200}]) | 140 |
downtimeTotal([{"start":0,"end":30}]) | 30 |
downtimeTotal([]) | 0 |
downtimeTotal([{"start":5,"end":10},{"start":20,"end":25},{"start":100,"end":130}]) | 40 |
Hint
Add up (end - start) across every block.
Reference solution in JavaScript
function downtimeTotal(blocks) {
let total = 0;
for (const b of blocks) total += b.end - b.start;
return total;
}