Activity Selection Problem | Greedy Algo | Returns count of the maximum set of activities that can be done by a single person , one at a time . ; Sort jobs according to finish time ; The first activity always gets selected ; Consider rest of the activities ; If this activity has start time greater than or equal to the finish time of previously selected activity , then select it ; Driver code
def MaxActivities ( arr , n ) : NEW_LINE INDENT selected = [ ] NEW_LINE Activity . sort ( key = lambda x : x [ 1 ] ) NEW_LINE i = 0 NEW_LINE selected . append ( arr [ i ] ) NEW_LINE for j in range ( 1 , n ) : NEW_LINE if arr [ j ] [ 0 ] >= arr [ i ] [ 1 ] : NEW_LINE INDENT selected . append ( arr [ j ] ) NEW_LINE i = j NEW_LINE DEDENT return selected NEW_LINE DEDENT Activity = [ [ 5 , 9 ] , [ 1 , 2 ] , [ 3 , 4 ] , [ 0 , 6 ] , [ 5 , 7 ] , [ 8 , 9 ] ] NEW_LINE n = len ( Activity ) NEW_LINE selected = MaxActivities ( Activity , n ) NEW_LINE print ( " Following ▁ activities ▁ are ▁ selected ▁ : " ) NEW_LINE print ( selected ) NEW_LINE