AmazonOA #316
Optimal Interval Difference
估计中等可在线判题
First merge all intersecting closed intervals into disjoint maximal segments; sharing an endpoint also counts as intersecting. Find the minimum gap between any two adjacent merged segments, that is, the start of the later segment minus the end of the earlier one. If fewer than two segments remain after merging, there is no gap to compare, and the output is −1 as this site's protocol specifies.
输入格式
The first line contains n, followed by n lines of start end. On this site 1≤n≤200000, −10^9≤start≤end≤10^9. The −1 encoding is only added for the case with no gap; single-segment inputs are not excluded.
输出格式
Output a single integer, the answer.
样例 1
输入:
3
1 4
2 5
8 11
输出:
3
样例 2
输入:
2
1 5
5 7
输出:
-1
样例 3
输入:
3
1 1
3 3
8 8
输出:
2
样例说明
The answers are 3, −1 and 2. The original description swaps the indices in start[i−1] and end[i]; following the original example 7−5=2, this site consistently uses the later segment's start minus the earlier segment's end.