1229. Meeting Scheduler
Last updated
Last updated
/**
* Time complexity :
* Space complexity :
*/
class Solution {
public List<Integer> minAvailableDuration(int[][] slots1,
int[][] slots2,
int duration) {
Comparator<int[]> comparator = (a, b) -> a[0]-b[0];
Arrays.sort(slots1, comparator);
Arrays.sort(slots2, comparator);
int ptr1 = 0, ptr2 = 0;
while(ptr1 < slots1.length && ptr2 < slots2.length) {
int[] slot1 = slots1[ptr1];
int[] slot2 = slots2[ptr2];
int start = Math.max(slot1[0], slot2[0]);
int end = Math.min(slot1[1], slot2[1]);
if((start + duration) <= end) {
return List.of(start, start+duration);
}
if(slot1[1] > slot2[1]) {
ptr2++;
} else {
ptr1++;
}
}
return List.of();
}
}/**
* Time complexity :
* Space complexity :
*/
class Solution {
public List<Integer> minAvailableDuration(int[][] slots1,
int[][] slots2,
int duration) {
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]);
for (int[] slot : slots1) {
if (slot[1] - slot[0] >= duration) {
pq.offer(slot);
}
}
for (int[] slot : slots2) {
if (slot[1] - slot[0] >= duration) {
pq.offer(slot);
}
}
List<Integer> results = new ArrayList<>();
while (pq.size() > 1) {
if (pq.poll()[1] - pq.peek()[0] >= duration) {
results.add(pq.peek()[0]);
results.add(pq.peek()[0] + duration);
return results;
}
}
return results;
}
}