Activity Selection Problem - Watermark
Activity Selection Problem - Watermark
Problem
We are given n activities with their start and finish times. We have to select the
maximum number of activities such that no two selected activities overlap.
Example
Approach
1. Sort all the activities by their finish times
2. Greedily select the first activity and jump on the next.
3. If the starting time of current activity is greater than the ending time of
previously selected activity, then take it otherwise ignore it.
Dry Run
Original start and end times
After sorting in increasing order
signed main() {
int n; cin >> n;
vector<pair<int,int>> times;
for(int i=0; i<n; i++) {
int st, fn;
cin >> st >> fn;
times.push_back({st,fn});
}
int ans = 1;
int previousEndTime = times[0].second;