Given an array of meeting time intervals intervals
where intervals[i] = [starti, endi]
, return the minimum number of conference rooms required.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2
Example 2:
Input: intervals = [[7,10],[2,4]]
Output: 1
Constraints:
1 <= intervals.length <= 104
0 <= starti < endi <= 106
A typical Chronological Ordering. We will need total order or both start and ends.
While we sort start and order individually. It's fine because : we don't really care about which meeting ends and we only need to know there is a meeting ends and room are available.
So the algorithm is like :
class Solution { public int minMeetingRooms(int[][] intervals) { int n = intervals.length; int res = 0; int[] starts = new int[n]; int[] ends = new int[n]; int index = 0; for (int[] interval : intervals) { starts[index] = intervals[index][0]; ends[index] = intervals[index++][1]; } Arrays.sort(starts); Arrays.sort(ends); for (int i = 0, j = 0; i < n; i++) { if (starts[i] < ends[j]) { res++; } else { j++; } } return res; } }