/*
If you had a list of appointments (each appointment has a begin time, an end time, and a boolean hasConflict), how would you efficiently go through them and set the hasConflict boolean for each. You cannot assume they are sorted in any way. Keep in mind that one appointment may be very long, etc.
*/
struct appointment {
int begin, end;
bool has_conflict;
}
bool is_overlap(appointment &a, appointment &b) {
if(a.end < b.begin || a.begin > b.end) return false;
return true;
}
// solution 1, brute force. compare every pair.
void update(vector<appointment>& va) {
if(va.empty()) return;
for(int i=0; i<va.size()-1; i++) {
for(int j=i+1; j<va.size(); j++) {
if(va[j].has_conflict == false && is_overlap(va[i], va[j]) == true)
va[i].has_conflict = va[j].has_conflict = true;
}
}
}
void update_better(vector<appointment>& va) {
if(va.empty()) return;
sort(va.begin(), va.end(), [](appointment& a, appointment&b) { return a.begin < b.begin; } );
for(int i=0; i<va.size()-1; i++) {
for(int j=i+1; j<va.size(); j++) {
if(is_overlap(va[i], va[j]) == false) break;
else if(va[j].has_conflict == false) {
va[i].has_conflict = va[j].has_conflict = true;
}
}
}
}