-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathEmployee Free Time.java
More file actions
37 lines (34 loc) · 1.06 KB
/
Employee Free Time.java
File metadata and controls
37 lines (34 loc) · 1.06 KB
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
/*
// Definition for an Interval.
class Interval {
public int start;
public int end;
public Interval() {}
public Interval(int _start, int _end) {
start = _start;
end = _end;
}
};
*/
class Solution {
public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
int lastIntervalEnd = -1;
List<Interval> result = new ArrayList<>();
PriorityQueue<Interval> pq = new PriorityQueue<>((a, b) -> a.start - b.start);
for (List<Interval> intervals : schedule) {
pq.addAll(intervals);
}
while (!pq.isEmpty()) {
Interval removed = pq.remove();
if (lastIntervalEnd != -1 && removed.start - lastIntervalEnd > 0) {
result.add(new Interval(lastIntervalEnd, removed.start));
}
int maxEnd = removed.end;
while (!pq.isEmpty() && pq.peek().start <= maxEnd) {
maxEnd = Math.max(maxEnd, pq.remove().end);
}
lastIntervalEnd = maxEnd;
}
return result;
}
}