Skip to content

Instantly share code, notes, and snippets.

@criskgl
Created October 28, 2019 20:47
Show Gist options
  • Save criskgl/d8e37570e8b7a5ba6e784f298575df20 to your computer and use it in GitHub Desktop.
Save criskgl/d8e37570e8b7a5ba6e784f298575df20 to your computer and use it in GitHub Desktop.
public int minMeetingRooms(int[][] intervals) {
int n = intervals.length;
int[] start = new int[n];
int[] end = new int[n];
for(int i = 0; i < n; i++){
start[i] = intervals[i][0];
end[i] = intervals[i][1];
}
Arrays.sort(start);
Arrays.sort(end);
int endItr = 0;
int rooms = 0;
for(int i = 0; i < n; i++){
//meeting is starting before the ending of another?
if(start[i] < end[endItr]){
rooms++;//if so, we need one more room!
}
//meeting starting after(or just after) other ends. No need for another room
else if(start[i] >= end[endItr]){
endItr++;//update next ending
}
}
return rooms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment