sample test case pass but
it throw wrong answer on submission.
what’s happening help !
Activity selection problems
i have resolved this problem.
please check try again after understanding this…
A basic activity selection problem which uses Greedy approach. The implementation mentioned here states to sort the activities in the increasing order of their finishing times. Initially, we choose the first activity. Now, starting from second activity, current activity can be selected if the finish time of previous selected activity is less than or equal to the starting time of current activity.
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b)
{
return a.second < b.second;
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n, a, b;
cin >> n;
pair<int, int> arr[n];
for (int i = 0; i < n; i++)
{
cin >> a >> b;
arr[i] = make_pair(a, b);
}
sort(arr, arr + n, cmp);
int ans = 1, chosen = 0;
for (int i = 1; i < n; i++)
{
if (arr[i].first >= arr[chosen].second)
{
ans++;
chosen = i;
}
}
cout << ans << endl;
}
return 0;
}
2 Likes