subject: pairing numbers and intervals
Let NUMBERS
be a list of n
integer numbers. The numbers are listed in no specific order. Duplicates are possible.
Let INTERVALS
be a list of m
intervals. The low and high limits of the intervals are integer numbers. The intervals are listed in no specific order. Duplicates are possible.
We want to pair the numbers and intervals as thus:
- A number can only be paired with an interval to which it belongs. For example,
4
and[3, 10]
can be paired,4
and[5, 10]
cannot. - A number cannot be paired with more than one interval.
- An interval cannot be paired with more than one number.
- The number of pairs is maximal.
- We are not interested in the pairs themselves, only in the (maximal) number of pairs.
To summarize:
- input =
NUMBERS
,INTERVALS
- output = maximal number of pairs
Example 1:
NUMBERS
=2
INTERVALS
=[4, 10]
- output =
0
Example 2:
NUMBERS
=2
INTERVALS
=[2, 10]
- output =
1
Example 3:
NUMBERS
=3, 7, 8, 12
INTERVALS
=[0, 10], [5, 15], [20, 25]
- output =
2
It is relatively simple to devise an O(n*m)
algorithm to solve this problem. I seriously doubt that there exist any O(n) + O(m)
algorithm, but what about an O(max(n*log(n), m*log(m), n*log(m), m*log(n)))
algorithm?