-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmployeeFreeTime.java
72 lines (61 loc) · 2.38 KB
/
EmployeeFreeTime.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package solutions;
import java.util.*;
// [Problem] https://leetcode.com/problems/employee-free-time
class EmployeeFreeTime {
// Min heap
// O(nlogn) time, O(n) space
// where n = total number of meetings in schedule
public List<Interval> employeeFreeTimeHeap(List<List<Interval>> schedule) {
Queue<Interval> employeeBusyTime = new PriorityQueue<>((a, b) -> a.start - b.start);
schedule.forEach(meetings -> employeeBusyTime.addAll(meetings));
List<Interval> availabilities = new ArrayList<>();
int startTime = employeeBusyTime.poll().end;
while (!employeeBusyTime.isEmpty()) {
Interval meeting = employeeBusyTime.poll();
if (meeting.start > startTime) {
availabilities.add(new Interval(startTime, meeting.start));
}
startTime = Math.max(startTime, meeting.end);
}
return availabilities;
}
// Array sorting
// O(nlogn) time, O(n) space
public List<Interval> employeeFreeTime(List<List<Interval>> schedule) {
List<Interval> employeeBusyTime = new ArrayList<>();
schedule.forEach(meetings -> employeeBusyTime.addAll(meetings));
employeeBusyTime.sort((a, b) -> a.start - b.start);
List<Interval> availabilities = new ArrayList<>();
int startTime = employeeBusyTime.get(0).end;
for (Interval meeting : employeeBusyTime) {
if (meeting.start > startTime) {
availabilities.add(new Interval(startTime, meeting.start));
}
startTime = Math.max(startTime, meeting.end);
}
return availabilities;
}
// Test
public static void main(String[] args) {
EmployeeFreeTime solution = new EmployeeFreeTime();
List<List<Interval>> input = List.of(
List.of(new Interval(1, 2), new Interval(5, 6)),
List.of(new Interval(1, 3)),
List.of(new Interval(4, 10))
);
List<Interval> actualOutput = solution.employeeFreeTime(input);
System.out.println("Actual output? " + actualOutput); // expectedOutput: [[3,4]]
}
}
class Interval {
public int start;
public int end;
public Interval(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public String toString() {
return "Interval{" + start + ", " + end + '}';
}
}