Problem: You are given some set of activities, with starting time and finish time. You have to tell the maximum number of activity can be performed by given person.
Algo:
Step1: sort the activity in increasing order according to finish time.
Step2: Start counter with 1. Because first activity is always selected.
Step 3: i=0;
Step3: Do the following steps for i<n, where n is the number of activities
if ith activity's finish time is less than (i+1)th activities start time.
increase the counter value
Reason Why we sort the activities according to finish time
we always print first activity, This activity always performed, But This gives confusion that, if first activity finish time is more than two activity which start after first activity.
for Example:
number of activity is 3
1 2 4
6 3 5
So only one activity will be performed, But there are two activity that can performed.
Here we forget that activity are sorted by their finish order.
So
2 4 1
3 5 6
So number of activity is two.
Now if we print first activity inside the for loop then last activity will not print, because condition if (s[j] >= f[i]) will fail, So we don't know which activity,
So print first activity outside for loop, Now in for loop activity which start time print, if (s[j] >= f[i]) print j activity, because i already printed.
Algo:
Step1: sort the activity in increasing order according to finish time.
Step2: Start counter with 1. Because first activity is always selected.
Step 3: i=0;
Step3: Do the following steps for i<n, where n is the number of activities
if ith activity's finish time is less than (i+1)th activities start time.
increase the counter value
Reason Why we sort the activities according to finish time
we always print first activity, This activity always performed, But This gives confusion that, if first activity finish time is more than two activity which start after first activity.
for Example:
number of activity is 3
1 2 4
6 3 5
So only one activity will be performed, But there are two activity that can performed.
Here we forget that activity are sorted by their finish order.
So
2 4 1
3 5 6
So number of activity is two.
Now if we print first activity inside the for loop then last activity will not print, because condition if (s[j] >= f[i]) will fail, So we don't know which activity,
So print first activity outside for loop, Now in for loop activity which start time print, if (s[j] >= f[i]) print j activity, because i already printed.