|
| 1 | +/** |
| 2 | + * 1229. Meeting Scheduler |
| 3 | + * https://leetcode.com/problems/meeting-scheduler/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * Given the availability time slots arrays slots1 and slots2 of two people and a meeting |
| 7 | + * duration duration, return the earliest time slot that works for both of them and is of |
| 8 | + * duration duration. |
| 9 | + * |
| 10 | + * If there is no common time slot that satisfies the requirements, return an empty array. |
| 11 | + * |
| 12 | + * The format of a time slot is an array of two elements [start, end] representing an |
| 13 | + * inclusive time range from start to end. |
| 14 | + * |
| 15 | + * It is guaranteed that no two availability slots of the same person intersect with each |
| 16 | + * other. That is, for any two time slots [start1, end1] and [start2, end2] of the same |
| 17 | + * person, either start1 > end2 or start2 > end1. |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * @param {number[][]} slots1 |
| 22 | + * @param {number[][]} slots2 |
| 23 | + * @param {number} duration |
| 24 | + * @return {number[]} |
| 25 | + */ |
| 26 | +var minAvailableDuration = function(slots1, slots2, duration) { |
| 27 | + slots1.sort((a, b) => a[0] - b[0]); |
| 28 | + slots2.sort((a, b) => a[0] - b[0]); |
| 29 | + |
| 30 | + let i = 0; |
| 31 | + let j = 0; |
| 32 | + |
| 33 | + while (i < slots1.length && j < slots2.length) { |
| 34 | + const overlapStart = Math.max(slots1[i][0], slots2[j][0]); |
| 35 | + const overlapEnd = Math.min(slots1[i][1], slots2[j][1]); |
| 36 | + |
| 37 | + if (overlapEnd - overlapStart >= duration) { |
| 38 | + return [overlapStart, overlapStart + duration]; |
| 39 | + } |
| 40 | + |
| 41 | + if (slots1[i][1] < slots2[j][1]) { |
| 42 | + i++; |
| 43 | + } else { |
| 44 | + j++; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return []; |
| 49 | +}; |
0 commit comments