A duty roster lists n shifts on a timeline, each a closed range [start, end]. Shifts that overlap or touch join into blocks. Between two consecutive blocks there is an empty gap. Report the total length of all such internal gaps (the empty space strictly between the earliest merged block and the latest one). Space before the first block or after the last block does not count.
Line 1: an integer n.
Next n lines: two integers start and end describing one shift.
A single integer: the total internal gap length between merged blocks.
Example 1
Input
3 0 2 5 7 9 10
Expected
5
Explanation
The three blocks are [0,2], [5,7], [9,10]; the gaps between them are 5-2=3 and 9-7=2, totalling 5.
Example 2
Input
2 0 4 2 6
Expected
0
Explanation
The shifts overlap into one block [0,6], so there are no internal gaps.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →