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
Java 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 downtimeTotal(List<Block> blocks) {
}
Worked examples
| Call | Result |
|---|---|
downtimeTotal(Main.<Block>ls(new Block(0, 60), new Block(120, 200))) | 140 |
downtimeTotal(Main.<Block>ls(new Block(0, 30))) | 30 |
downtimeTotal(Main.<Block>ls()) | 0 |
downtimeTotal(Main.<Block>ls(new Block(5, 10), new Block(20, 25), new Block(100, 130))) | 40 |
Hint
Add up (end - start) across every block.
Reference solution in Java
int downtimeTotal(List<Block> blocks) {
int total = 0;
for (Block b : blocks) total += b.end - b.start;
return total;
}