#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/*
Given an array of meeting time interval consiting of start and end times
[[s1, e1], [s2, e2], [s3, e3]] (si < ei), find the mininum number of
conference rooms required.
For example:
Given [[0, 30], [5, 10], [15, 20]]
return 2.
*/
struct Interval {
int start;
int end;
Interval(int a, int b) : start(a), end(b) {}
};
static bool sorter(int a, int b) {
return abs(a) < abs(b);
}
int minMeetingRooms(vector<Interval>& intervals) {
int res = 0;
if(intervals.size() == 0) return res;
if(intervals.size() == 1) return res + 1;
vector<int> times;
for(int i = 0; i < intervals.size(); ++i) {
times.push_back(intervals[i].start);
times.push_back(-intervals[i].end);
}
sort(times.begin(), times.end(), sorter);
int count = 0;
for(int i = 0; i < times.size(); ++i) {
if(times[i] >= 0) count++;
else count--;
res = max(res, count);
}
return res;
}
int main(void) {
Interval time_1(0, 30);
Interval time_2(5, 10);
Interval time_3(15, 20);
vector<Interval> intervals;
intervals.push_back(time_1);
intervals.push_back(time_2);
intervals.push_back(time_3);
cout << minMeetingRooms(intervals) << endl;
}
Another variation to find the best meeting time. Best meeting time is absolutely the maxNumber of houses required if the conference runs in parallel.
#include "header.h"
using namespace std;
struct Interval{
int start;
int end;
Interval(int s, int e) : start(s), end(e) {}
};
static inline bool sorter(int a, int b) {
return abs(a) < abs(b);
}
int bestMeetingPoint(vector<Interval>& intervals) {
vector<int> times;
for(int i = 0; i < intervals.size(); ++i) {
times.push_back(intervals[i].start);
times.push_back(-intervals[i].end);
}
int maxCount = 0;
int bestTime = 0;
sort(times.begin(), times.end(), sorter);
int count = 0;
for(int i = 0; i < times.size(); ++i) {
if(times[i] >= 0) count++;
else count--;
if(maxCount < count) {
maxCount = count;
bestTime = times[i];
}
}
return bestTime;
}
int main(void) {
vector<Interval> intervals{Interval{1, 3}, Interval{2, 7}, Interval{4, 8}, Interval{5, 9}};
cout << bestMeetingPoint(intervals) << endl;
}