Open In App

Meeting Scheduler for Two Persons

Last Updated : 30 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

You have given the availability time slots ‘slots1’ and ‘slots2’ of two people and a meeting duration ‘d’, for any i, slots[i] = {starti , endi}. return the earliest time slot that works for both of them and is of ‘d’ duration.

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

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.

Examples:

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

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

Approach: To solve the problem, follow the below idea:

This problem can be solved using two pointers, We will sort both slots1 and slots2 by the start time first, then initialize two pointers, each of which points to the beginning of the two arrays. From there, we will compare the two slots, and move one pointer at a time if the common slot is smaller than duration.

To decide which pointer should be incremented, we will always move the one that ends earlier. Assuming that we are comparing slots1[i] and slots2[j] and slots1[i][1] > slots2[j][1], we would always choose to move the pointer j. The reason is that, as both slots are sorted, if slots1[i][1] > slots2[j][1], we know slots1[i+1][0] > slots2[j][1] so that there will be no intersection between slots1[i+1] and slots2[j].

Steps to solve this problem:

  • Sort both slots (slots1 and slots2) are on start times.
  • Keep two pointers (pointer1 and pointer2) and initialized to start at the beginning of both sorted arrays.
  • Intersection Check:
    • While both pointers are within the bounds of their respective arrays:
    • Find the intersection boundaries (intersectLeft and intersectRight) of the current slots pointed to by pointer1 and pointer2.
    • If the duration ‘d’ of the common slot (intersectRight – intersectLeft) is greater than or equal to the required duration:
      • Return the earliest common time slot.
    • Move the pointer of the slot that ends earlier.
  • If no common time slot satisfies the duration requirement, return an empty array.

Below is the implementation of the above approach:

C++
#include <bits/stdc++.h>
using namespace std;

vector<int>
minAvailableDuration(vector<vector<int> >& slots1,
                     vector<vector<int> >& slots2,
                     int d)
{
    // Sorting the slots arrays based on start times
    sort(slots1.begin(), slots1.end());
    sort(slots2.begin(), slots2.end());

    int pointer1 = 0, pointer2 = 0;

    while (pointer1 < slots1.size()
           && pointer2 < slots2.size()) {
        // Finding the boundaries of the intersection, or
        // the common slot
        int intersectLeft
            = max(slots1[pointer1][0], slots2[pointer2][0]);
        int intersectRight
            = min(slots1[pointer1][1], slots2[pointer2][1]);
        if (intersectRight - intersectLeft >= d) {
            return { intersectLeft,
                     intersectLeft + d };
        }
        // Always move the pointer of the slot that ends
        // earlier
        if (slots1[pointer1][1] < slots2[pointer2][1]) {
            pointer1++;
        }
        else {
            pointer2++;
        }
    }
    return {}; // Return an empty array if no common time
               // slot found
}

int main()
{

    vector<vector<int> > slots1
        = { { 10, 50 }, { 60, 120 }, { 140, 210 } };
    vector<vector<int> > slots2 = { { 0, 15 }, { 60, 70 } };
    int d = 8;
    vector<int> result
        = minAvailableDuration(slots1, slots2, d);

    if (!result.empty()) {
        cout << "Earliest common time slot: [" << result[0]
             << ", " << result[1] << "]" << endl;
    }
    else {
        cout << "No common time slot available." << endl;
    }

    return 0;
}
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static List<Integer>
    minAvailableDuration(List<List<Integer> > slots1,
                         List<List<Integer> > slots2, int d)
    {
        // Sorting the slots arrays based on start times
        Collections.sort(slots1,
                         (a, b) -> a.get(0) - b.get(0));
        Collections.sort(slots2,
                         (a, b) -> a.get(0) - b.get(0));

        int pointer1 = 0, pointer2 = 0;

        while (pointer1 < slots1.size()
               && pointer2 < slots2.size()) {
            // Finding the boundaries of the intersection,
            // or the common slot
            int intersectLeft
                = Math.max(slots1.get(pointer1).get(0),
                           slots2.get(pointer2).get(0));
            int intersectRight
                = Math.min(slots1.get(pointer1).get(1),
                           slots2.get(pointer2).get(1));
            if (intersectRight - intersectLeft >= d) {
                List<Integer> result = new ArrayList<>();
                result.add(intersectLeft);
                result.add(intersectLeft + d);
                return result;
            }
            // Always move the pointer of the slot that ends
            // earlier
            if (slots1.get(pointer1).get(1)
                < slots2.get(pointer2).get(1)) {
                pointer1++;
            }
            else {
                pointer2++;
            }
        }
        return new ArrayList<>(); // Return an empty list if
                                  // no common time slot
                                  // found
    }

    public static void main(String[] args)
    {
        List<List<Integer> > slots1 = new ArrayList<>();
        slots1.add(List.of(10, 50));
        slots1.add(List.of(60, 120));
        slots1.add(List.of(140, 210));

        List<List<Integer> > slots2 = new ArrayList<>();
        slots2.add(List.of(0, 15));
        slots2.add(List.of(60, 70));

        int d = 8;
        List<Integer> result
            = minAvailableDuration(slots1, slots2, d);

        if (!result.isEmpty()) {
            System.out.println(
                "Earliest common time slot: ["
                + result.get(0) + ", " + result.get(1)
                + "]");
        }
        else {
            System.out.println(
                "No common time slot available.");
        }
    }
}

// This code is contributed by shivamgupta310570
Python3
def minAvailableDuration(slots1, slots2, d):
    # Sorting the slots arrays based on start times
    slots1.sort()
    slots2.sort()

    pointer1, pointer2 = 0, 0

    while pointer1 < len(slots1) and pointer2 < len(slots2):
        # Finding the boundaries of the intersection, or the common slot
        intersectLeft = max(slots1[pointer1][0], slots2[pointer2][0])
        intersectRight = min(slots1[pointer1][1], slots2[pointer2][1])
        if intersectRight - intersectLeft >= d:
            return [intersectLeft, intersectLeft + d]
        # Always move the pointer of the slot that ends earlier
        if slots1[pointer1][1] < slots2[pointer2][1]:
            pointer1 += 1
        else:
            pointer2 += 1
    #
    return []  # Return an empty list if no common time slot found

if __name__ == "__main__":
    slots1 = [[10, 50], [60, 120], [140, 210]]
    slots2 = [[0, 15], [60, 70]]
    d = 8
    result = minAvailableDuration(slots1, slots2, d)

    if result:
        print("Earliest common time slot:", result)
    else:
        print("No common time slot available.")
        
# This code is contributed by Ayush Mishra.
JavaScript
function minAvailableDuration(slots1, slots2, d) {
    // Sorting the slots arrays based on start times
    slots1.sort((a, b) => a[0] - b[0]);
    slots2.sort((a, b) => a[0] - b[0]);

    let pointer1 = 0;
    let pointer2 = 0;

    while (pointer1 < slots1.length && pointer2 < slots2.length) {
        // Finding the boundaries of the intersection, or the common slot
        const intersectLeft = Math.max(slots1[pointer1][0], slots2[pointer2][0]);
        const intersectRight = Math.min(slots1[pointer1][1], slots2[pointer2][1]);
        if (intersectRight - intersectLeft >= d) {
            return [intersectLeft, intersectLeft + d];
        }
        // Always move the pointer of the slot that ends earlier
        if (slots1[pointer1][1] < slots2[pointer2][1]) {
            pointer1++;
        } else {
            pointer2++;
        }
    }
    return []; // Return an empty array if no common time slot found
}

// Example usage
const slots1 = [[10, 50], [60, 120], [140, 210]];
const slots2 = [[0, 15], [60, 70]];
const d = 8;
const result = minAvailableDuration(slots1, slots2, d);

if (result.length > 0) {
    console.log("Earliest common time slot:", result);
} else {
    console.log("No common time slot available.");
}

Output
Earliest common time slot: [60, 68]

Time Complexity: O(n log n)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads