Given a set of busy time inter | CareerCup
http://www.careercup.com/question?id=5739192933941248
Given a set of busy time intervals of two people as in a calendar, find the free time intervals of both the people so as to arrange a new meeting
input: increasing sequence of pair of numbers
per1: (1,5) (10, 14) (19,20) (27,30)
per2: (3,5) (12,15) (18, 21) (23, 24)
ouput: (6,9) (16,17) (22,22) (25,26)
We don't have to sort: just merge them first.
Merging works because we are searching for places in which nothing is scheduled. If even one of the individuals have something scheduled, then there isn't free time.
https://github.com/andreytim/jafar/blob/master/problems/src/main/java/com/andreytim/jafar/problems/sortsearch/P717_IntervalArraysMergeGaps.java
https://www.quora.com/Given-a-set-of-busy-time-intervals-of-two-people-as-in-a-calendar-what-is-the-algorithmic-approach-to-find-the-free-time-intervals-of-both-the-people-so-as-to-arrange-a-new-meeting
Merge all the intervals(per1 + per2) in a single list of intervals. Sort the list using start time of the intervals as the key.
Now, merge the overlapping intervals. Now iterate over the list with merged overlapping intervals and check for the gaps between the intervals. The gaps will be the free time intervals.
Read full article from Given a set of busy time inter | CareerCup
http://www.careercup.com/question?id=5739192933941248
Given a set of busy time intervals of two people as in a calendar, find the free time intervals of both the people so as to arrange a new meeting
input: increasing sequence of pair of numbers
per1: (1,5) (10, 14) (19,20) (27,30)
per2: (3,5) (12,15) (18, 21) (23, 24)
ouput: (6,9) (16,17) (22,22) (25,26)
We don't have to sort: just merge them first.
Merging works because we are searching for places in which nothing is scheduled. If even one of the individuals have something scheduled, then there isn't free time.
public static int[][] getFreeTimes(int[][] person1, int[][] person2){
//merge
int[][] list = merge(person1, person1);
//do the rest
ArrayList<int[]> results = new ArrayList<int[]>();
int workingEnd = 1;
for(int[] event : list){
if(event[0] <= workingEnd){
if(event[1] >= workingEnd){
workingEnd = event[1]+1;
}
}
else{
results.add(new int[]{workingEnd, event[1]-1}); // should be event[0]-1
workingEnd = event[1]+1;
}
}
return results.toArray();
}
for (int i = 0; i < sortedMeetings.size(); i++) {
Meeting duration1 = null;
Meeting duration2 = null;
if (i < sortedMeetings.size() - 1) {
duration1 = sortedMeetings.get(i);
duration2 = sortedMeetings.get(i + 1);
if (duration1.getEndTime() < duration2.getStartTime()) {
freeSlots.add(new Meeting(duration1.getEndTime(), duration2.getStartTime()));
}
}
}
//TODO: check the logichttps://github.com/andreytim/jafar/blob/master/problems/src/main/java/com/andreytim/jafar/problems/sortsearch/P717_IntervalArraysMergeGaps.java
https://www.quora.com/Given-a-set-of-busy-time-intervals-of-two-people-as-in-a-calendar-what-is-the-algorithmic-approach-to-find-the-free-time-intervals-of-both-the-people-so-as-to-arrange-a-new-meeting
Merge all the intervals(per1 + per2) in a single list of intervals. Sort the list using start time of the intervals as the key.
Now, merge the overlapping intervals. Now iterate over the list with merged overlapping intervals and check for the gaps between the intervals. The gaps will be the free time intervals.
void get_free_times(const vector<pair<int, int> > &per1, const vector<pair<int, int> > &per2)
{
int i;
int s, e;
vector<pair<int, int> > allintervals;
vector<pair<int, int> > merged;
pair<int, int> temp;
for(i=0; i<per1.size(); i++)
{
allintervals.push_back(make_pair(per1[i].first, per1[i].second));
}
for(i=0; i<per2.size(); i++)
{
allintervals.push_back(make_pair(per2[i].first, per2[i].second));
}
sort(allintervals.begin(), allintervals.end());
for(i=0; i<allintervals.size(); i++)
{
if(temp.first==0 && temp.second==0)
{
temp.first=allintervals[i].first;
temp.second=allintervals[i].second;
}
if(allintervals[i].first>=temp.first && allintervals[i].first<=temp.second)
{
temp.second=max(allintervals[i].second, temp.second);
}
else
{
merged.push_back(temp);
temp.first=allintervals[i].first;
temp.second=allintervals[i].second;
}
}
merged.push_back(temp);
for(i=0; i<merged.size()-1; i++)
{
s=merged[i].second+1;
e=0;
if(s<merged[i+1].first)
{
e=merged[i+1].first-1;
}
if(e)
{
cout<<"("<<s<<", "<<e<<")"<<endl;
}
}
}
Read full article from Given a set of busy time inter | CareerCup