A team's combined calendar is given as n busy intervals, each the half-open interval [start, end). After merging every overlapping or touching busy interval into solid blocks, the team is free during the gaps that lie strictly between consecutive blocks. Report the total length of these in-between free gaps. Time before the earliest busy moment and after the latest busy moment does not count.
Line 1: an integer n.
Next n lines: two integers start end (with start < end).
A single integer: the total length of the free gaps between busy blocks.
Example 1
Input
3 0 2 5 7 9 10
Expected
5
Explanation
Busy blocks are [0,2), [5,7), [9,10). The free gaps between them are [2,5) of length 3 and [7,9) of length 2, totalling 5.
Example 2
Input
2 0 5 2 8
Expected
0
Explanation
The two intervals overlap into a single block [0,8), so there is no gap between blocks: total 0.
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 →