forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployee Free Time.java
More file actions
32 lines (30 loc) · 893 Bytes
/
Employee Free Time.java
File metadata and controls
32 lines (30 loc) · 893 Bytes
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
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
/*
*/
class Solution {
public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
List<Interval> ans = new ArrayList<>();
PriorityQueue<Interval> pq = new PriorityQueue<>((a,b) -> a.start - b.start);
schedule.forEach(e -> pq.addAll(e));
Interval temp = pq.poll();
while (!pq.isEmpty()) {
if (temp.end < pq.peek().start) {
ans.add(new Interval(temp.end, pq.peek().start));
temp = pq.poll();
}
else {
temp = temp.end < pq.peek().end ? pq.peek() : temp;
pq.poll();
}
}
return ans;
}
}