-
Notifications
You must be signed in to change notification settings - Fork 5
/
MeetingRooms2.java
42 lines (38 loc) · 1.14 KB
/
MeetingRooms2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*https://www.lintcode.com/problem/919*/
/**
* Definition of Interval:
* public class Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
* }
*/
public class Solution {
/**
* @param intervals: an array of meeting time intervals
* @return: the minimum number of conference rooms required
*/
public int minMeetingRooms(List<Interval> intervals) {
// Write your code here
int count = 0, i, prev, n = intervals.size(), scheduledCount = 0;
Collections.sort(intervals,(a,b)->(a.end == b.end ? a.start-b.start : a.end-b.end));
boolean[] scheduled = new boolean[n];
while (scheduledCount != n)
{
++count;
prev = Integer.MAX_VALUE;
for (i = intervals.size()-1; i >= 0; --i)
{
if (intervals.get(i).end <= prev && !scheduled[i])
{
scheduled[i] = true;
++scheduledCount;
prev = intervals.get(i).start;
}
}
}
return count;
}
}