1006. Flowers in Bloom

A botanical garden records each flower as a pair [a, b]: the bloom is open on every whole day from a to b, both included. If the pair is listed the other way round (a > b), treat it as [b, a].

Visitors arrive on days given by people. For each visitor, in the original order, report how many flowers are in bloom on the arrival day.

Return an int[] of the same length as people. With m flowers and q visitors, aim for O((m + q) log m) time.

Example 1

Input:
flowers = [[7,12],[3,9],[15,15],[10,20]]people = [2,9,12,15,21]
Output:
[0,2,2,2,0]
Explanation:

At time 9 the first two intervals are open, at 12 the first and last are open, at 15 the third and fourth are open, and times 2 and 21 see none.

Example 2

Input:
flowers = [[4,6],[6,6],[2,5]], people = [6,1,5]
Output:
[2,0,2]
Explanation:

Time 6 lies in both [4,6] and [6,6] (end times are inclusive), time 1 is before everything, and time 5 is inside [4,6] and [2,5].

Constraints

1 ≤ m ≤ 105, 1 ≤ q ≤ 700

0 ≤ a, b, people[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O((m+q) log m)
Space
O(m)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…