A different approach and I am not sure of the performance of the whole thing, but the idea is to flatten out the ranges defined by your schedules in a BitArray that signals if a specific minute has been already used by a previous schedule.
Technically it is also not a 'one liner' answer albeit from the stand point of the calling code it is just one line
So suppose to have defined a class ScheduleList
public class ScheduleList : List<Schedule>
{
private BitArray ba = new BitArray(1440);
// Define own Add method to Add a Schedule to the list
// Or override the predefined one....
public Schedule Add(int sh, int sm, int eh, int em)
{
Schedule s = new Schedule();
s.StartTime = new DateTime(1, 1, 1, sh, sm, 0);
s.EndTime = new DateTime(1, 1, 1, eh, em, 0);
// Of course, having full control on the Add phase, you
// could easily enforce your policy at this point.
// You could not accept a new schedule if the time slot is busy
// but we could ignore it at this point
this.Add(s);
return s;
}
public bool IsTimeSlotBusy(Schedule s)
{
int sidx = Convert.ToInt32(TimeSpan.FromMinutes((s.StartTime.Hour * 60) + s.StartTime.Minute).TotalMinutes);
int eidx = Convert.ToInt32(TimeSpan.FromMinutes((s.EndTime.Hour * 60) + s.EndTime.Minute).TotalMinutes);
for (int x = sidx; x <= eidx; x++)
{
if (ba.Get(x)) return true;
ba.Set(x, true);
}
return false;
}
}
now you could write something like this
ScheduleList schedules = new ScheduleList();
schedules.Add(12, 0, 12, 30);
schedules.Add(15, 10, 15, 30);
schedules.Add(12, 0, 23, 59);
schedules.Add(14, 0, 23, 59);
// The fake 'one liner'
bool overlap = schedules.Any(s => schedules.IsTimeSlotBusy(s));
Console.WriteLine(overlap);
Waiting for the mathematicians here to show me why this is worst. (Not ironic, I really wish to know if this is bad from a mathematical point of view)