Friday, October 9, 2020

LeetCode [1229] Meeting Scheduler

 1229. Meeting Scheduler

Medium

Given the availability time slots arrays slots1 and slots2 of two people and a meeting duration duration, return the earliest time slot that works for both of them and is of duration duration.

If there is no common time slot that satisfies the requirements, return an empty array.

The format of a time slot is an array of two elements [start, end] representing an inclusive time range from start to end.  

It is guaranteed that no two availability slots of the same person intersect with each other. That is, for any two time slots [start1, end1] and [start2, end2] of the same person, either start1 > end2 or start2 > end1.

 

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: []

 

Constraints:

  • 1 <= slots1.length, slots2.length <= 10^4
  • slots1[i].length, slots2[i].length == 2
  • slots1[i][0] < slots1[i][1]
  • slots2[i][0] < slots2[i][1]
  • 0 <= slots1[i][j], slots2[i][j] <= 10^9
  • 1 <= duration <= 10^6 
 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
class Solution {
    public List<Integer> minAvailableDuration(int[][] slots1, int[][] slots2, int duration) {
        int n1 = slots1.length, n2 = slots2.length;
        int i1 = 0, i2 = 0, l1 = 0, l2 = 0, r1 = 0, r2 = 0;
        List<Integer> list = new ArrayList<>();
        Arrays.sort(slots1, (a,b)->a[0]-b[0]);
        Arrays.sort(slots2, (a,b)->a[0]-b[0]);
        while(i1<n1 && i2<n2){
            l1 = slots1[i1][0];
            l2 = slots2[i2][0];
            r1 = slots1[i1][1];
            r2 = slots2[i2][1];
            if(r1-l1<duration){
                i1++;
            }else if(r2-l2<duration){
                i2++;
            }else{
                int l = Math.max(l1, l2);
                int r = Math.min(r1, r2);
                if(r-l>=duration){
                    list.add(l);
                    list.add(l+duration);
                    return list;
                }else{
                    if(r2>r1){
                        i1++;
                    }else{
                        i2++;
                    }
                }
            }
        }
        return list;
    }
}

No comments:

Post a Comment