A radio station schedules n broadcast windows on a minute-numbered timeline. Each window covers the half-open range from its start minute up to (but not including) its end minute. Windows may overlap, touch, be nested, or be empty (start equal to end). Compute the total number of minutes that lie inside at least one window; a minute covered by several windows still counts only once.
Line 1: an integer n.
Next n lines: two integers start and end describing one window.
A single integer: the total number of covered minutes (the length of the union of all windows).
Example 1
Input
3 1 4 2 6 8 10
Expected
7
Explanation
Windows [1,4) and [2,6) overlap into [1,6), covering 5 minutes; [8,10) covers 2 more. Total 7.
Example 2
Input
2 0 5 5 9
Expected
9
Explanation
The two windows only touch at minute 5, so together they cover [0,9): 5 + 4 = 9 minutes with nothing double-counted.
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 →