Common Slot for Meeting of Two Persons

 

You are given two lists of availability time slots, slt1[][] and slt2[][], for two people. Each slot is represented as [start, end], and it is guaranteed that within each list, no two slots overlap (i.e., for any two intervals, either start1>end2 or start2>end1).
Given a meeting duration d, return the earliest common time slot of length at least d. If no such slot exists, return an empty array.

Input: slt1[][] = [[10,50], [60,120], [140,210]], slt2[][] = [[0,15], [60,70]], d = 8
Output: [60,68]
Explanation: The only overlap is [60,70] (10 minutes), which is enough for an 8-minute meeting, so answer is [60,68]

Input: slt1[][] = [[10,50], [60,120], [140,210]], slt2[][] = [[0,15], [60,70]], d = 12
Output: []
Explanation: The only overlap is [60, 70] (10 minutes), but 12 minutes are needed, so no valid slot exists.

 

class gfg{

    public int[] commonMeeting(int[] [] slot1,int[] [] slot2,int d){

        Arrays.sort(slot1,(x,y) -> Integer.compare(x[0],y[0]));

        Arrays.sort(slot2,(x,y) -> Integer.compare(x[0],y[0])); 

        int p1 = 0;

        int p2 = 0;

        while(p1 < slot1.length && p2 < slot2.length){

                int left = Math.max(slot1[p1][0],slot2[p2][0]);

                int right = Math.min(slot1[p1][1],slot2[p2][1]);

                if(right - left >= d){

                        return new int[] {left,left + d};

                }

                if(slot1[p1][1] < slot2[p2][1]){

                        p1++;

            }else{

                        p2++;

        }

    }

    return new int[0];

}

 

Comments

Popular Posts