Showing posts with label Event Scheduling. Show all posts
Showing posts with label Event Scheduling. Show all posts

Wednesday, January 4, 2012

Find all the conflicting appointments from a given list of n appointments.

You are given 'n' appointments. Each appointment contains startime and endtime. You have to return all conflicting appointments efficiently starttime and endtime can range from a few min to few years.


Approach:

Suppose there are 5 events:
E1 => 13 : 15
E2 => 18 : 20
E3 => 11 : 24
E4 => 19 : 27
E5 =>  4  : 12

Now sort the events with start time:


E5: 4 : 12
E3: 11 : 24
E1: 13 : 15
E2: 18 : 20
E4: 19: 27


Now take the end time of first Event (E5) ie 12 and check in the start time the first event whose start time is greater than 12, in the above example E1 - with start time 13 is the event.
Now we can safely say that all the events less than E1 and greater than E5 are conflicting with E5 - which is E3 in our case.
With the same above logic, E3 is conflicting with E1, E2 and E4.
Time complexity = O(nlogn) for sorting the events on start time + O(nlogn) for searching a all the conflicting events for a given event Ei (1 <= i <= n).
So total time complexity: O(nlogn)


Output of the below code:

Events sorted with start time:
5: 4:12
3: 11:24
1: 13:15
2: 18:20
4: 19:27
Conflicts are: 
1 <> 3 
2 <> 3 4 
3 <> 5 1 2 4 
4 <> 3 2 
5 <> 3 




Thursday, March 17, 2011

Event Scheduling for N Employees in a month

You are given N ranges of date offsets when N employees are present in an organization. Something like
1-4 (i.e. employee will come on 1st, 2nd, 3rd and 4th day )
2-6
8-9
..
1-14
You have to organize an event on minimum number of days such that each employee can attend the event at least twice.


APPROACH:
All employees have to attend atleast 2 times.
So I keep a count of 2 for every employee initially.


Then I initialize an array (days) of 30 days maintaining a count of number of employees who can attend on a particular day.


I take the maximum of the days array. For all the employees, who can attend on that day I reduced their counts in the first array.
When the count becomes zero for any employee, I delete him from the days array.


I keeping running this loop until all employees have count <=0.
Input via console: N (no. of Employees) and then N rows of ranges corresponding to the N employees.
Sample Input:

15
2 4
3 8
5 20
24 30
1 10
9 15
19 23
20 30
1 5
2 10
1 15
1 10
5 25
15 30
7 17
Sample output:

Printing event days: 
5
7
15
20
21
3
4
25
24
9