## Amazon Interview Question

**Country:**United States

A greedy algorithm doesn't guarantee scheduling the maximum number of meetings. This problem can be mapped to a conflict graph: every meeting can be mapped to a vertex in a graph. If two meetings have overlap (conflicts), they share an (undirected) edge in the graph. Now the question becomes a variant of vertex coloring problem where two adjacent vertices don't share the same color (two conflict meetings cannot share the same room).

To optimally solve this, a backtracking solution can do the job, but with two cases:

1. if the graph is k-colorable, just return N

2. if the graph is not k-colorable, increase k until it's m-colorable (m>k). Now you will get m set of vertices. Sort them with non-increasing order of set sizes and return the total size of the first k sets.

This can transformed to :-

- Construct each interval as a node.

- Connect two nodes if those intervals overlap, to build an "undirected graph"

(Note: graph could be a forest also(disconnected components))

- Now problem is :- how many of N nodes can be colored with K colors ?

@hieu.pham overlapped meeting are adjacent to each other.

if you have 'k' meeting rooms, then you can assign each vertex 1 color out of these 'k'.

all the adjacent vertices must not have that same color, but among themselves they can have a different same color if they don't have a direct edge between them.

example:

M1: 13:00-14:00 - Color 1

M2: 12:30-13:30 - Color 2

M3: 13:30-14:00 - Color 2

(M2,M3) are adjacent to M1

M2 & M3 are not adjacent so the can share the same color

This is the problem of job scheduling using N jobs and K factory line. There is a greedy solution for this problem.

Sort jobs based on their finish time and select the jobs for the lines (rooms), if there is more than one line (room) that job can fit inside then select the line with the least interval of the last finished job on that line and the current job. If the job doesn't fit then drop that job and go to the next job.

Idea is to sort the Intervals with absolute value of startTime and EndTime, but in real store the -EndTime in the array. Count the maximum value of consecutive StartTime and in the that max will be the result. A quick code is below:

```
public static int maximumMeetingScheduler(ArrayList<Interval> intervals) {
ArrayList<Integer> convertedIntervals = new ArrayList<>();
for(Interval interval : intervals) {
convertedIntervals.add(interval.startTime);
convertedIntervals.add(-interval.endTime);
}
Comparator<Integer> comp = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Math.abs(o1).compareTo(Math.abs(o2));
}
}
Collections.sort(convertedIntervals, comp);
int max = 0;
int result = max;
for(int i=0;i<convertedIntervals.size();i++) {
if(convertedIntervals.get(i)>0) {
max++;
} else {
max--;
}
if(result<max) result = max;
}
return result;
}
```

Just sort based on start time and schedule meetings in the rooms. Loop N/K times.

However, i have one question on this, what if we find out that one of the meeting start time will be passed by the time the current running meetings end. Do we ever schedule this meeting. Is this a valid use case or the test cases given are such that this case will never arise?

This is the problem of job scheduling:

- Nooreddin July 09, 2015There are N jobs and K lines.

Sort all the jobs (meetings) by their finish time and then start selection from the jobs that finishes first putting it in one room and select the next job put it in a room that does not overlap with other jobs if there are more than one lines (rooms) then select the one that has the least finished time interval to the start of this job.