Skip to content

Commit f841907

Browse files
committed
Add Intervals/759_Employee_Free_Time.java
1 parent 1f5df37 commit f841907

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Intervals/759_Employee_Free_Time.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
// Definition for an Interval.
3+
class Interval {
4+
public int start;
5+
public int end;
6+
7+
public Interval() {}
8+
9+
public Interval(int _start, int _end) {
10+
start = _start;
11+
end = _end;
12+
}
13+
};
14+
*/
15+
16+
class Solution {
17+
public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
18+
List<Interval> result = new ArrayList<>();
19+
20+
PriorityQueue<Interval> pq = new PriorityQueue<>(Comparator.comparing(i -> i.start));
21+
schedule.forEach(i -> pq.addAll(i));
22+
23+
Interval prev = pq.poll();
24+
25+
while (!pq.isEmpty()) {
26+
Interval curr = pq.poll();
27+
28+
if (prev.end < curr.start) {
29+
result.add(new Interval(prev.end, curr.start));
30+
prev = curr;
31+
} else {
32+
prev.end = Math.max(prev.end, curr.end);
33+
}
34+
}
35+
36+
return result;
37+
}
38+
}

0 commit comments

Comments
 (0)