Given ‘N’ events each with a ‘start’ and ‘end’ time in the form of intervals such that it represents a booking on the half-open interval [start, end), i.e., the range of real numbers x such that start <= x < end. Initially, the calendar has no events. A new event can be added to the calendar, if adding the event will not cause a triple booking. A triple booking happens when three events have some non-empty intersection (i.e., there exists a time common to all the 3 events).
Your task is to process the 'N' events, and for each of the events, add that event to the calendar and return ‘True’, if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return ‘False’ and do not add that event to the calendar.
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains an integer ‘N’, which represents the total number of events.
Then the next ‘N’ lines contain two space-separated integers 'Start' and 'End', denoting the starting and ending times of the 'N' intervals.
Output format:
For each test case, print 'N' space-separated 'True' or 'False' denoting the status of each event. (‘True’ if the addition of an event to the calendar does not cause triple booking, ‘False’ otherwise).
Output for each test case is printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given functions.
1 <= T <= 10
1 <= N <= 1000
0 <= Start , End <= 10^9
Time Limit: 1 sec
2
6
10 20
50 60
10 40
5 15
5 10
25 55
4
1 10
20 30
5 6
8 9
True True True False True True
True True True True
For the first test case,
The first two events can be booked. The third event can be booked because it causes conflict with the first event only once.The fourth event (5, 15) can't be booked, because it would result in a triple booking. The fifth event (5, 10) can be booked, as it does not use time [1, 10) or overlapping instead it overlaps with [5, 15) which is overlapped once. The sixth event (25, 55) can be booked, as it will overlap with the third event with the time [10, 40) which is also overlapped only once.
For the second test case,
The first two events can be booked. The third event will also be booked as it will be overlap with the first event. The fourth event will be booked because it will overlap with the first event only and not with the third event.
2
4
1 10
20 30
5 6
2 7
5
1 10
20 30
1 15
8 9
10 15
True True True False
True True True False True
In the calendar, there will be only two types of events one that occurs at least one time and the other that occurs exactly two times?
Algorithm:
O(N ^ 2), where ‘N’ is the total number of events.
We are iterating over the ‘doubleBookedEvents’ and the ‘Events’ array which will take O(N) time for each one of the ‘N’ events. Hence, the overall total Time Complexity is O(N ^ 2).
O(N), where ‘N’ is the number of events.
O(N) space is required to store the events in the ‘Events’ array and the events that are conflicting twice in the ‘doubleBookedEvents’ array.
Hence, the overall Space Complexity is O(N).