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.
Examples:
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.
We start by sorting both slots by start time. Then, use two pointer to step through both slots. For each pair, calculate the overlap as [max(start₁, start₂), min(end₁, end₂)]. If the overlap is at least d long, return that slot; otherwise, advance the pointer for the interval that finishes first.
#include <bits/stdc++.h>
using namespace std;
// Function to compute the earliest common
// time slot of length at least d
vector<int> commonSlot(vector<vector<int>>& slt1,
vector<vector<int>>& slt2, int d)
{
// Sorting the slt1[][] and slt2[][]
// arrays based on start times
sort(slt1.begin(), slt1.end());
sort(slt2.begin(), slt2.end());
int p1 = 0, p2 = 0;
while (p1 < slt1.size() && p2 < slt2.size()) {
// Finding the boundaries of the intersection,
// or the common slot
int left = max(slt1[p1][0], slt2[p2][0]);
int right = min(slt1[p1][1], slt2[p2][1]);
if (right - left >= d) {
return { left, left + d };
}
// Always move the pointer of the
// slot that ends earlier
if (slt1[p1][1] < slt2[p2][1]) {
p1++;
} else {
p2++;
}
}
// Return an empty array if no common time slot found
return {};
}
int main()
{
vector<vector<int>> slt1 = { {10, 50}, {60, 120}, {140, 210} };
vector<vector<int>> slt2 = { {0, 15}, {60, 70} };
int d = 8;
vector<int> result = commonSlot(slt1, slt2, d);
if (!result.empty()) {
cout << "[" << result[0] << ", " << result[1] << "]" << endl;
} else {
cout << "[]" << endl;
}
return 0;
}
import java.util.*;
import java.io.*;
class solution {
public int[] commonSlot(int[][] slt1, int[][] slt2, int d) {
// Sorting the slt1[][] and slt2[][] arrays based on start times
Arrays.sort(slt1, (x, y) -> Integer.compare(x[0], y[0]));
Arrays.sort(slt2, (x, y) -> Integer.compare(x[0], y[0]));
int p1 = 0, p2 = 0;
while (p1 < slt1.length && p2 < slt2.length) {
// Finding the boundaries of the intersection, or the common slot
int left = Math.max(slt1[p1][0], slt2[p2][0]);
int right = Math.min(slt1[p1][1], slt2[p2][1]);
if (right - left >= d) {
return new int[]{left, left + d};
}
// Always move the pointer of the slot that ends earlier
if (slt1[p1][1] < slt2[p2][1])
p1++;
else
p2++;
}
// Return an empty array if no common time slot found
return new int[0];
}
}
public class GfG {
public static void main(String[] args) throws Exception {
// Example test case
int[][] slt1 = new int[][] {
{10, 50},
{60, 120},
{140, 210}
};
int[][] slt2 = new int[][] {
{0, 15},
{60, 70}
};
int d = 8;
solution sol = new solution();
int[] result = sol.commonSlot(slt1, slt2, d);
if (result.length == 0)
System.out.println("[]");
else
System.out.println("[" + result[0] + ", " + result[1] + "]");
}
}
def common_slot(slt1, slt2, d):
# Sorting the 2D arrays based on start times
slt1.sort(key=lambda x: x[0])
slt2.sort(key=lambda x: x[0])
p1, p2 = 0, 0
while p1 < len(slt1) and p2 < len(slt2):
# Finding the boundaries of the intersection,
# or the common slot
left = max(slt1[p1][0], slt2[p2][0])
right = min(slt1[p1][1], slt2[p2][1])
if right - left >= d:
return [left, left + d]
# Always move the pointer of the slot
# that ends earlier
if slt1[p1][1] < slt2[p2][1]:
p1 += 1
else:
p2 += 1
return [] # Return an empty list if no common time slot found
slt1 = [[10, 50], [60, 120], [140, 210]]
slt2 = [[0, 15], [60, 70]]
d = 8
result = common_slot(slt1, slt2, d)
print(result)
using System;
using System.Collections.Generic;
public class solution {
public int[][] commonSlot(int[,] slt1, int[,] slt2, int d) {
// Get the lengths of the input arrays.
int n1 = slt1.GetLength(0);
int n2 = slt2.GetLength(0);
// Initialize an array to hold the common slots.
List<int[]> commonSlots = new List<int[]>();
// Iterate through the slots to find common
// available time.
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
// Finding the boundaries of the intersection,
// or the common slot.
int left = Math.Max(slt1[i, 0], slt2[j, 0]);
int right = Math.Min(slt1[i, 1], slt2[j, 1]);
if (right - left >= d) {
commonSlots.Add(new int[] { left, left + d });
}
}
}
return commonSlots.ToArray(); // Convert List to array before returning.
}
}
public class GfG {
public static void Main() {
int[,] slt1 = { {10, 50}, {60, 120}, {140, 210} };
int[,] slt2 = { {0, 15}, {60, 70} };
int d = 8;
solution sol = new solution();
int[][] result = sol.commonSlot(slt1, slt2, d);
if (result.Length == 0)
Console.WriteLine("[]");
else
Console.WriteLine("[" + result[0][0] + ", " + result[0][1] + "]");
}
}
function commonSlot(slt1, slt2, d) {
// Sorting the 2D arrays based on start times
slt1.sort((x, y) => x[0] - y[0]);
slt2.sort((x, y) => x[0] - y[0]);
let p1 = 0, p2 = 0;
while (p1 < slt1.length && p2 < slt2.length) {
// Finding the boundaries of the intersection,
// or the common slot
let left = Math.max(slt1[p1][0], slt2[p2][0]);
let right = Math.min(slt1[p1][1], slt2[p2][1]);
if (right - left >= d) {
return [left, left + d];
}
// Always move the pointer of the slot that ends earlier
if (slt1[p1][1] < slt2[p2][1])
p1++;
else
p2++;
}
// Return an empty array if no common time slot found
return [];
}
const slt1 = [[10, 50], [60, 120], [140, 210]];
const slt2 = [[0, 15], [60, 70]];
const d = 8;
const result = commonSlot(slt1, slt2, d);
console.log(result);
Output
[60, 68]
Time Complexity: O(n log n), n is length of larger slot
Auxiliary Space: O(1)