user1071840
user1071840

Reputation: 3592

Given a schedule find all conflicting appointments for a doctors office

You are given 'n' appointments. Each appointment contains startime and endtime. You have to retun all conflicting appointments efficiently starttime and endtime can range from a few min to few years. I saw the post: FInd overlapping appointments in O(n) time? which is very similar to this. But a doubt remains.

My question is that if we have a sorted schedule given, say : (6:30-6:35), (6:40-7:10) (7:25-7:40), (7:35-7:50) and so on.

Can we not traverse the schedule list and make sure that the next appointment should begin ONLY after the previous one is over? In that case we're applying a very tight bound on the possible time for scheduling an appointment. In this example, we know that the appointment beginning at 7:35 is before last appointment ends (7:40) so it is a conflict. Do we really need to complicate the solution by creating a tree or hash map as mentioned in the most rated solution in link for similar question?

Please point out cases which can bypass this check and prove this condition invalid.

Upvotes: 1

Views: 3810

Answers (2)

verdesmarald
verdesmarald

Reputation: 11866

In the linked answer, the list appointments is not sorted, hence the more complicated solution. You are correct that if the appointments are sorted you can just traverse the list and keep track of the latest end time seen so far.

Also, note that sorting the list is O(n lg n), so you can't just sort it and then traverse it if you want an O(n) solution.

Upvotes: 1

aquinas
aquinas

Reputation: 23796

Of course the solution can be performed in O(n) time if as you say "we have a sorted schedule given." The question you referenced is assuming the times are NOT sorted.

Upvotes: 0

Related Questions