Activity selection problem with K persons | Python3 program for the above approach ; Function to find maximum shops that can be visited by K persons ; Store opening and closing time of shops ; Sort the pair of array ; Stores the result ; Stores current number of persons visiting some shop with their ending time ; Check if current shop can be assigned to a person who 's  already visiting any other shop ; Checks if there is any person whose closing time <= current shop opening time ; Erase previous shop visited by the person satisfying the condition ; Insert new closing time of current shop for the person satisfying a1he condition ; Increment the count by one ; In case if no person have closing time <= current shop opening time but there are some persons left ; Finally pr the ans ; Driver Code ; Given starting and ending time ; Given K and N ; Function call
from bisect import bisect_left NEW_LINE def maximumShops ( opening , closing , n , k ) : NEW_LINE INDENT a = [ [ 0 , 0 ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] [ 0 ] = opening [ i ] NEW_LINE a [ i ] [ 1 ] = closing [ i ] NEW_LINE DEDENT a = sorted ( a ) NEW_LINE count = 1 NEW_LINE st = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT flag = False NEW_LINE if ( len ( st ) == 0 ) : NEW_LINE INDENT ar = list ( st . keys ( ) ) NEW_LINE it = bisect_left ( ar , a [ i ] [ 0 ] ) NEW_LINE if ( it != 0 ) : NEW_LINE INDENT it -= 1 NEW_LINE if ( ar [ it ] <= a [ i ] [ 0 ] ) : NEW_LINE INDENT del st [ it ] NEW_LINE st [ a [ i ] [ 1 ] ] = 1 NEW_LINE count += 1 NEW_LINE flag = True NEW_LINE DEDENT DEDENT DEDENT if ( len ( st ) < k and flag == False ) : NEW_LINE INDENT st [ a [ i ] [ 1 ] ] = 1 NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = [ 1 , 8 , 3 , 2 , 6 ] NEW_LINE E = [ 5 , 10 , 6 , 5 , 9 ] NEW_LINE K , N = 2 , len ( S ) NEW_LINE print ( maximumShops ( S , E , N , K ) ) NEW_LINE DEDENT