Length of smallest meeting that can be attended | Python3 program to implement the above approach ; Function to find the minimum time to attend exactly one meeting ; Stores minimum time to attend exactly one meeting ; Sort entrance [ ] array ; Sort exit [ ] time ; Traverse meeting [ ] [ ] ; Stores start time of current meeting ; Stores end time of current meeting ; Find just greater value of u in entrance [ ] ; Find just greater or equal value of u in entrance [ ] ; Stores enter time to attend the current meeting ; Stores exist time after attending the meeting ; Update start lies in range [ 0 , m - 1 ] and end lies in the range [ 0 , p - 1 ] ; Update ans ; Return answer ; Driver Code ; Stores interval of meeting ; Stores entrance timings ; Stores exit timings ; Stores total count of meetings ; Stores total entrance timings ; Stores total exit timings ; Minimum time
from bisect import bisect_left , bisect_right NEW_LINE import sys NEW_LINE def minTime ( meeting , n , entrance , m , exit , p ) : NEW_LINE INDENT ans = sys . maxsize NEW_LINE entrance = sorted ( entrance ) NEW_LINE exit = sorted ( exit ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT u = meeting [ i ] [ 0 ] NEW_LINE v = meeting [ i ] [ 1 ] NEW_LINE it1 = bisect_right ( entrance , u ) NEW_LINE it2 = bisect_left ( exit , v ) NEW_LINE start = it1 - 1 NEW_LINE end = it2 NEW_LINE if ( start >= 0 and start < m and end >= 0 and end < p ) : NEW_LINE INDENT ans = min ( ans , exit [ end ] - entrance [ start ] ) NEW_LINE DEDENT DEDENT if ans >= sys . maxsize : NEW_LINE INDENT ans = - 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT meeting = [ [ 15 , 19 ] , [ 5 , 10 ] , [ 7 , 25 ] ] NEW_LINE entrance = [ 4 , 13 , 25 , 2 ] NEW_LINE exit = [ 10 , 25 ] NEW_LINE n = len ( meeting ) NEW_LINE m = len ( entrance ) NEW_LINE p = len ( exit ) NEW_LINE print ( minTime ( meeting , n , entrance , m , exit , p ) ) NEW_LINE DEDENT