Length of smallest meeting that can be attended
Last Updated :
22 Jun, 2021
Given a 2D array arr[][] of the form {start, end} representing the start and end time of N meetings, also given two arrays entrance[] and exist[] representing the opening and closing times of the meeting room respectively, the task is to find the minimum time for which a meeting can be attended. If it is not possible to attend any meeting, then print -1.
Examples:
Input: arr[][] = {{15, 19}, {5, 10}, {7, 25}}, entrance[] = {4, 13, 25, 2}, exist[] = {10, 21}
Output: 6
Explanation:
Meeting 1: Enter at the time 13, attend the meeting in the interval (15, 19) and exit at the time 21. Therefore, total time spent in the meeting = 21 – 13 = 8.
Meeting 2: Enter at the time 4, attend the meeting in (5, 10) and exit at the time 10. Therefore, total time spent in the meeting = 10 – 4 = 6.
Meeting 3: Enter at the time 4, attend the meeting in the interval (7, 25). But after the time 25 there is no closing time. Therefore, total time spent is infinite.
Therefore, minimum units of time that can be spent to attend a meeting is 6.
Input: arr[][] = {{1, 2}}, entrance[] = {1, 2}, exist[] = {3, 4}
Output: 2
Naive Approach: The simplest approach to solve this problem is to traverse arr[][] and for each interval {starti, endi}, find the value which just smaller than or equal to arr[i][0] in the array entrance[]. Also, find the value which is just greater than or equal to arr[i][1] in the array exist[]. Finally, print the minimum time to attend exactly one meeting.
Time Complexity: O(N*max(P, M)), where M and P are the size of entrance[]and exist[] arrays.
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach the idea is to use Sorting Algorithm and Binary Search technique.
Follow the steps below to solve the problem:
- Sort the arrays entrance[] and exist[] in increasing order.
- Initialize a variable ans to store the minimum time to attend exactly one meeting.
- Traverse the array and for each interval of the meetings, find the value which is just smaller than or equal to starti in the entrance[] array using upper_bound and find the value which is just greater than or equal to endiin the exist[] array using lower_bound.
- Finally, print the minimum time to attend exactly one meeting.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minTime( int meeting[][2], int n,
vector< int > entrance, int m,
vector< int > & exit , int p)
{
int ans = INT_MAX;
sort(entrance.begin(), entrance.end());
sort( exit .begin(), exit .end());
for ( int i = 0; i < n; i++) {
int u = meeting[i][0];
int v = meeting[i][1];
auto it1
= upper_bound(entrance.begin(),
entrance.end(), u);
auto it2
= lower_bound( exit .begin(),
exit .end(), v);
int start = it1
- entrance.begin() - 1;
int end = it2 - exit .begin();
if (start >= 0 && start < m &&
end >= 0 && end < p)
ans = min(ans,
exit [end] - entrance[start]);
}
return ans >= INT_MAX ? -1 : ans;
}
int main()
{
int meeting[][2]
= { { 15, 19 }, { 5, 10 }, { 7, 25 } };
vector< int > entrance = { 4, 13, 25, 2 };
vector< int > exit = { 10, 25 };
int n = ( sizeof (meeting))
/ sizeof (meeting[0]);
int m = entrance.size();
int p = exit .size();
cout << minTime(meeting, n, entrance,
m, exit , p)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static Vector<Integer> exit =
new Vector<>();
static int minTime( int meeting[][], int n,
int [] entrance, int m,
int p)
{
int ans = Integer.MAX_VALUE;
Arrays.sort(entrance);
Collections.sort(exit);
for ( int i = 0 ; i < n; i++)
{
int u = meeting[i][ 0 ];
int v = meeting[i][ 1 ];
int it1 = upper_bound(entrance, 0 ,
entrance.length, u);
int it2 = lowerBound(exit, 0 ,
exit.size(), v);
int start = it1 - 1 ;
int end = it2 ;
if (start >= 0 && start < m &&
end >= 0 && end < p)
ans = Math.min(ans,
exit.get(end) -
entrance[start]);
}
return ans >= Integer.MAX_VALUE ?
- 1 : ans;
}
static int upper_bound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low +
(high - low) / 2 ;
if (a[middle] > element)
high = middle;
else
low = middle + 1 ;
}
return low;
}
static int lowerBound(Vector<Integer> vec,
int low, int high,
int element)
{
int [] array =
new int [vec.size()];
int k = 0 ;
for (Integer val : vec)
{
array[k] = val;
k++;
}
while (low < high)
{
int middle = low +
(high - low) / 2 ;
if (element > array[middle])
{
low = middle + 1 ;
} else
{
high = middle;
}
}
return low;
}
public static void main(String[] args)
{
int meeting[][] = {{ 15 , 19 },
{ 5 , 10 },
{ 7 , 25 }};
int []entrance = { 4 , 13 , 25 , 2 };
exit.add( 10 );
exit.add( 25 );
int n = meeting.length;
int m = entrance.length;
int p = exit.size();
System.out.print(minTime(meeting, n,
entrance, m,
p) + "\n" );
}
}
|
Python3
from bisect import bisect_left, bisect_right
import sys
def minTime(meeting, n, entrance, m, exit, p):
ans = sys.maxsize
entrance = sorted (entrance)
exit = sorted (exit)
for i in range (n):
u = meeting[i][ 0 ]
v = meeting[i][ 1 ]
it1 = bisect_right(entrance, u)
it2 = bisect_left(exit, v)
start = it1 - 1
end = it2
if (start > = 0 and start < m and
end > = 0 and end < p):
ans = min (ans, exit[end] -
entrance[start])
if ans > = sys.maxsize:
ans = - 1
return ans
if __name__ = = '__main__' :
meeting = [ [ 15 , 19 ], [ 5 , 10 ], [ 7 , 25 ] ]
entrance = [ 4 , 13 , 25 , 2 ]
exit = [ 10 , 25 ]
n = len (meeting)
m = len (entrance)
p = len (exit)
print (minTime(meeting, n, entrance,
m, exit, p))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static List< int > exit = new List< int >();
static int minTime( int [,]meeting, int n,
int [] entrance, int m,
int p)
{
int ans = int .MaxValue;
Array.Sort(entrance);
exit.Sort();
for ( int i = 0; i < n; i++)
{
int u = meeting[i, 0];
int v = meeting[i, 1];
int it1 = upper_bound(entrance, 0,
entrance.Length, u);
int it2 = lowerBound(exit, 0,
exit.Count, v);
int start = it1 - 1;
int end = it2;
if (start >= 0 && start < m &&
end >= 0 && end < p)
ans = Math.Min(ans,
exit[end] -
entrance[start]);
}
return ans >= int .MaxValue ?
-1 : ans;
}
static int upper_bound( int [] a, int low,
int high, int element)
{
while (low < high)
{
int middle = low + (high - low) / 2;
if (a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
static int lowerBound(List< int > vec,
int low, int high,
int element)
{
int [] array = new int [vec.Count];
int k = 0;
foreach ( int val in vec)
{
array[k] = val;
k++;
}
while (low < high)
{
int middle = low + (high - low) / 2;
if (element > array[middle])
{
low = middle + 1;
}
else
{
high = middle;
}
}
return low;
}
public static void Main(String[] args)
{
int [,]meeting = { { 15, 19 },
{ 5, 10 },
{ 7, 25 } };
int []entrance = { 4, 13, 25, 2 };
exit.Add(10);
exit.Add(25);
int n = meeting.GetLength(0);
int m = entrance.Length;
int p = exit.Count;
Console.Write(minTime(meeting, n,
entrance, m,
p) + "\n" );
}
}
|
Javascript
<script>
let exit = [];
function minTime(meeting, n, entrance, m, p)
{
let ans = Number.MAX_VALUE;
(entrance).sort( function (a, b){ return a - b;});
(exit).sort( function (a, b){ return a - b;});
for (let i = 0; i < n; i++)
{
let u = meeting[i][0];
let v = meeting[i][1];
let it1 = upper_bound(entrance, 0,
entrance.length, u);
let it2 = lowerBound(exit, 0,
exit.length, v);
let start = it1 - 1;
let end = it2;
if (start >= 0 && start < m &&
end >= 0 && end < p)
ans = Math.min(ans,
exit[end] -
entrance[start]);
}
return ans >= Number.MAX_VALUE ?
-1 : ans;
}
function upper_bound(a, low, high, element)
{
while (low < high)
{
let middle = low +
Math.floor((high - low) / 2);
if (a[middle] > element)
high = middle;
else
low = middle + 1;
}
return low;
}
function lowerBound(vec, low, high, element)
{
let array = new Array(vec.length);
let k = 0;
for (let val = 0; val < vec.length; val++)
{
array[k] = vec[val];
k++;
}
while (low < high)
{
let middle = low +
Math.floor((high - low) / 2);
if (element > array[middle])
{
low = middle + 1;
}
else
{
high = middle;
}
}
return low;
}
let meeting = [ [ 15, 19 ],
[ 5, 10 ],
[ 7, 25 ] ];
let entrance = [ 4, 13, 25, 2 ];
exit.push(10);
exit.push(25);
let n = meeting.length;
let m = entrance.length;
let p = exit.length;
document.write(minTime(meeting, n, entrance,
m, p) + "<br>" );
</script>
|
Time Complexity: O(N * max(logP, logM)) where M and P are the lengths of entrance[] and exist[] arrays.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...