AmazonOA #52
Minimum Sets (Adding One Delivery Zone)
Estimated MediumJudge-ready
Topics (AI-labelled)Prefix & RangesTwo Pointers
Given some existing closed intervals, add exactly one closed interval [a,b] with 0≤b−a≤k. Intervals merge when they intersect or share an endpoint; integer endpoints that differ by 1 without a common point do not count as touching. Find the minimum number of connected blocks.
Input
The first line contains n k (1≤n≤200000, 1≤k≤10⁹), followed by n lines of a b (1≤a≤b≤10⁹). The endpoints of the new interval can be any integers you choose.
Output
Output a single integer answer.
Example 1
Input:
5 2
1 5
2 4
6 6
7 14
16 19
Output:
2
Example 2
Input:
1 1
1 2
Output:
1
Example 3
Input:
2 4
1 1
5 5
Output:
1
Example notes
Example 1: there are 4 blocks after merging; adding [5,7] of length 2 connects the first three blocks, leaving 2. Example 2: the new interval can be placed inside an existing interval, so there is still 1 block. Example 3: adding [1,5] of length 4 connects the two points into 1 block.
Editorial
Loading solution…