
1. In sorted order interval [T1, T2] comes before [T3, T4] if either T1 < T3, or (T1 == T3 and T2 < T4).
2. An interval [T1, T2] represents, time T1, T1+1, T1+2, ... T2, i.e all integers between T1, T2 both T1 and T2 inclusive.
3. For simplicity, we represent the list of intervals in a 1D array where every two numbers show an interval, i.e list [1, 3, 5, 7, 9, 11] represent intervals [1, 3], [5, 7] and [9, 11]
4. It is guaranteed that there will be at least one interval where all problem setters are free.
Let suppose there are 3 problem setters, their working intervals are represented by the following list of lists, ‘Schedules’: [[1, 2, 5, 6], [1,2], [5, 10]], where the ith interval of a setter is represented by 2*i and (2*i+1)th integer in their respective list, I.e. Problem Setter with an id 0, works in the intervals [1, 2], [5, 6]. Problem Setter with an id 1, work in the interval [1,2]. Problem Setter with id 2, works in the interval [5, 10],
In this example, the time intervals where setter 0 is free are [0, 0], [3, 4], [7, 10^8]
And the time intervals where setter 1 is free are [0, 0], [3, 10^8].
And the time intervals where setter 2 is free are [0, 4], [11, 10^8].
We can clearly observe that time intervals, where all 3 setters are free are, [0, 0], [3, 4], and [11, 10^8].
Thus we should output a list [0, 0, 3, 4, 11, 10^8] that represents these lists in 1D array form as described in notes. It can be shown easily, that this is the minimum possible length list of intervals representing common free time.
The first line of input contains an integer ‘T’ denoting the number of test cases, then ‘T’ test cases follow.
The first line of each test case consists of a single integer ‘N’ representing the number of problem setters.
Then 2*‘N’ line follows, which gives the ‘schedule’ of each of the problem setters. The 2*i+1th line consist of single even integer ‘Ki’ representing the number of intervals of ith problem setter and (2*i+2)th line consist of 2*Ki space-separated integers representing the list of intervals in a 1-D array.
For each test case, in a separate line print the smallest and sorted list of non-overlapping intervals that give the common free time of ‘N’ problem setters. I.e if there are ‘K’ such intervals, then you need to print 2*K space-separated integers representing this list in a 1-D array.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 1000
1 <= K <= 1000
Time limit: 1 sec
We will iterate from 0 to 10^8, and for each of the time points, we will iterate in the schedule of each of the ‘N’ problem setters and check whether they are free at that time or not. We will insert all these time-intervals in a list and group the consecutive time in a single interval.
We will iterate from 0 to 10^8, and for each of the time points, we will use binary search in the schedule (Note schedule of each setter is sorted) of each of the ‘N’ problem setters to check whether they are free at that time or not. We will insert all these time-intervals in a list and group the consecutive time in a single interval.
We will create a boolean array of size ‘10^8’ and fill it with true. This array represents whether all setters are free at a particular time or not. We will then iterate over each interval present in the schedule of each of the N problem setters. While iterating over the interval, we will mark false at the index corresponding to that time point of that interval in the boolean array to indicate all setters are not free at that interval.
Note that there are N*K intervals, where ‘N’ is the number of problem setters and ‘K’ is the maximum number of intervals in the schedule of any setter. Since each interval can be represented by only 2 integers, thus we will have at most 2*N*K integers representing intervals in which at least problem setter is working, Similarly we will have 2*N*K integers intervals in which some problem setter is not working (interval between two working intervals is a free interval for a problem setter), Thus overall we will have at max 4*N*K+2 integers representing busy or free intervals of problem setters. (We also include 0 and 10^8 ), So we can map all these integers to some unique integer between 0 to 4*N*K+2. To do this we will first create a list consisting of all these integers and then sort this list in non-decreasing order. After this, we will map each of these integers to some smaller integers such that their relative magnitude will remain the same, and replace all the intervals in the schedule using these mapped values. This technique is called coordinate compression.
After coordinate compression, let say that the maximum integer in intervals is ‘M’, clearly M <= 4*N*K+2, We will then create an integer array of size ‘M’+1, let's name it busySetters, such that busySetters[t] will give a number of Problem Setters that are busy at a time ‘t’. Note ‘t’ will be a compressed value. Initially, we fill this array with 0.
After that, we iterate over all the intervals in schedules of each of the problem setters and for each interval [x, y] we will increment all the indexes between [x, y] by ‘1’ as one more problem setter is busy at these times. We can do it efficiently by only incrementing busySetters[x] by 1 and decrement busySetters[y+1] by 1 for each interval [x, y]. The number of Problem Setters that are busy at any time ‘t’ will be the sum of first ‘t’ integers in busySetters. This technique is also known as a difference array.
Clearly, all setters are free at time ‘t’, if busySetters[t] = 0, Lastly we will combine consecutive free times in intervals.
First Digit One
Special Digit Numbers
Minimize Maximum Adjacent Distance
Sorted Doubly Linked List to Balanced BST
Minimized Maximum of Products Distributed to Any Store