/*
// Definition for an Interval.
class Interval {
public:
int start;
int end;
Interval() {}
Interval(int _start, int _end) {
start = _start;
end = _end;
}
};
*/
class Solution {
public:
vector<Interval> employeeFreeTime(vector<vector<Interval>> schedule) {
}
};
/**
* Definition for an Interval.
* type Interval struct {
* Start int
* End int
* }
*/
func employeeFreeTime(schedule [][]*Interval) (ans []*Interval) {
a := []int{}
for _, s := range schedule {
for _, p := range s {
a = append(a, p.Start<<1, p.End<<1|1) // 最低位表示区间左右
}
}
sort.Ints(a)
s := 0
for i, v := range a {
s += 1 - v&1<<1 // 根据左右 +1 或 -1
if s == 0 && i+1 < len(a) {
ans = append(ans, &Interval{v >> 1, a[i+1] >> 1})
}
}
return
}
/*
// 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> employeeFreeTime1(List<List<Interval>> avails) {
int OPEN = 0, CLOSE = 1;
List<int[]> events = new ArrayList();
for (List<Interval> employee: avails)
for (Interval iv: employee) {
events.add(new int[]{iv.start, OPEN});
events.add(new int[]{iv.end, CLOSE});
}
Collections.sort(events, (a, b) -> a[0] != b[0] ? a[0]-b[0] : a[1]-b[1]);
List<Interval> ans = new ArrayList();
int prev = -1, bal = 0;
for (int[] event: events) {
// event[0] = time, event[1] = command
if (bal == 0 && prev >= 0)
ans.add(new Interval(prev, event[0]));
bal += event[1] == OPEN ? 1 : -1;
prev = event[0];
}
return ans;
}
// 优先队列
public List<Interval> employeeFreeTime(List<List<Interval>> avails) {
List<Interval> ans = new ArrayList();
PriorityQueue<Job> pq = new PriorityQueue<Job>((a, b) ->
avails.get(a.eid).get(a.index).start -
avails.get(b.eid).get(b.index).start);
int ei = 0, anchor = Integer.MAX_VALUE;
for (List<Interval> employee: avails) {
pq.offer(new Job(ei++, 0));
anchor = Math.min(anchor, employee.get(0).start);
}
while (!pq.isEmpty()) {
Job job = pq.poll();
Interval iv = avails.get(job.eid).get(job.index);
if (anchor < iv.start)
ans.add(new Interval(anchor, iv.start));
anchor = Math.max(anchor, iv.end);
if (++job.index < avails.get(job.eid).size())
pq.offer(job);
}
return ans;
}
}
class Job {
int eid, index;
Job(int e, int i) {
eid = e;
index = i;
}
}
"""
# Definition for an Interval.
class Interval:
def __init__(self, start: int = None, end: int = None):
self.start = start
self.end = end
"""
class Solution:
# 扫描线
def employeeFreeTime(self, schedule: '[[Interval]]') -> '[Interval]':
OPEN, CLOSE = 0, 1
events = []
for emp in schedule:
for iv in emp:
events.append((iv.start, OPEN))
events.append((iv.end, CLOSE))
events.sort()
ans = []
prev = None
bal = 0
for t, cmd in events:
if bal == 0 and prev is not None:
ans.append(Interval(prev, t))
bal += 1 if cmd is OPEN else -1
prev = t
return ans
# 优先队列
def employeeFreeTime2(self, schedule: '[[Interval]]') -> '[Interval]':
ans = []
pq = [(emp[0].start, ei, 0) for ei, emp in enumerate(avails)]
heapq.heapify(pq)
anchor = min(iv.start for emp in avails for iv in emp)
while pq:
t, e_id, e_jx = heapq.heappop(pq)
if anchor < t:
ans.append(Interval(anchor, t))
anchor = max(anchor, avails[e_id][e_jx].end)
if e_jx + 1 < len(avails[e_id]):
heapq.heappush(pq, (avails[e_id][e_jx+1].start, e_id, e_jx+1))
return ans