[LeetCode] 1229. Meeting Scheduler

Schedule a meeting. Meaning of the questions is to schedule two people and a duration long, the schedule of interval representation, please return two people may have the opportunity to length of time a duration of what the conference interval Yes. example,

Example 1:

Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 8
Output: [60,68]

Example 2:

Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 12
Output: []

Analogy, such as the first example, they want to open a long 8-minute meeting, the two following schedule, the result returned is 60 - 68 minutes time the two have met.

This problem does not need to consider invalid the case, such as the start time interval greater than the end time interval, it would be better to deal with some. The idea is still the classic scan line . discussion inside the highest votes in answer [4.4.2020] to a priority queue of practice, I have not completely figured out. His idea is to get rid of both men after an invalid interval (interval itself is less than the time duration of the meeting), the two men join all valid interval pq, pq are sorted by the start time of the interval. Pop a first interval, comparing the end time interval of [1] is greater than equal to the top of the stack interval [0] + duration. I do not understand is where he has been here is how to ensure the top of the heap of pop elements and elements of the current top of the stack does not come from the same person. Which students teach me please, thank you very much.

I am here to provide a non-pq approach. Intervals of the first two terms of the start time sequence, after traversing the pointers are double Intervals two people, while looking for the possible start and end of each interval in which, if satisfied start + duration <= end of the recording start and the current end. If there is no such case it returns an empty list.

Time O (nlogn) - sort

Space O (1)

Java implementation

 1 class Solution {
 2     public List<Integer> minAvailableDuration(int[][] slots1, int[][] slots2, int duration) {
 3         PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparing(a -> a[0]));
 4         for (int[] s : slots1) {
 5             if (s[1] - s[0] >= duration) {
 6                 pq.offer(s);
 7             }
 8         }
 9         for (int[] s : slots2) {
10             if (s[1] - s[0] >= duration) {
11                 pq.offer(s);
12             }
13         }
14         while (pq.size() > 1) {
15             if (pq.poll()[1] >= pq.peek()[0] + duration) {
16                 return Arrays.asList(pq.peek()[0], pq.peek()[0] + duration);
17             }
18         }
19         return Arrays.asList();
20     }
21 }

 

JavaScript implementation

 1 /**
 2  * @param {number[][]} slots1
 3  * @param {number[][]} slots2
 4  * @param {number} duration
 5  * @return {number[]}
 6  */
 7 var minAvailableDuration = function(slots1, slots2, duration) {
 8     slots1.sort((a, b) => a[0] - b[0]);
 9     slots2.sort((a, b) => a[0] - b[0]);
10     let m = slots1.length;
11     let n = slots2.length;
12     let i = 0;
13     let j = 0;
14     while (i < m && j < n) {
15         let start = Math.max(slots1[i][0], slots2[j][0]);
16         let end = Math.min(slots1[i][1], slots2[j][1]);
17         if (start + duration <= end) {
18             return [start, start + duration];
19         } else if (slots1[i][1] < slots2[j][1]) {
20             i++;
21         } else {
22             j++;
23         }
24     }
25     return [];
26 };

 

Guess you like

Origin www.cnblogs.com/aaronliu1991/p/12635738.html