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
function MaxActivities ( arr , n ) { let selected = [ ] ; Activity = Activity . sort ( function ( a , b ) { return a [ 1 ] - b [ 1 ] ; } ) ; let i = 0 selected . push ( arr [ i ] ) ; for ( let j = 1 ; j < n ; j ++ ) { if ( arr [ j ] [ 0 ] >= arr [ i ] [ 1 ] ) { selected . push ( arr [ j ] ) ; i = j ; } } return selected ; } Activity = [ [ 5 , 9 ] , [ 1 , 2 ] , [ 3 , 4 ] , [ 0 , 6 ] , [ 5 , 7 ] , [ 8 , 9 ] ] ; n = Activity . length ; selected = MaxActivities ( Activity , n ) ; document . write ( " " ) console . log ( selected ) for ( let i = 0 ; i < selected . length ; i ++ ) document . write ( " " + selected [ i ] + " " )