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