AmazonOA #302
Minimum Retailers
Estimated MediumJudge-ready
Topics (AI-labelled)Binary SearchPrefix & Ranges
Each merchant corresponds to a closed interval. Delete the fewest merchants so that the remaining merchants include a center merchant whose interval intersects the interval of every other remaining merchant. Sharing an endpoint counts as intersecting. All intervals do not need a common point, and it is not enough for them to be connected as a whole.
Input
The first line contains n, followed by n lines of start end. The original bounds are 1≤n≤100000, 1≤start≤end≤10^9, and duplicate intervals are allowed.
Output
Output a single integer, the answer.
Example 1
Input:
5
3 8
4 5
6 7
10 11
12 13
Output:
2
Example 2
Input:
2
1 2
2 3
Output:
0
Example 3
Input:
3
1 1
3 3
5 5
Output:
2
Example notes
The answers are 2, 0 and 2. In the first example the center [3,8] touches two disjoint small intervals at once, so the maximum simultaneous coverage cannot be used instead.
Editorial
Loading solution…