fork download
  1. //Activity Selection
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6. vector<pair<int,int>> arr = {{1,2},{2,3},{3,4},{1,3}};
  7.  
  8. sort(arr.begin(), arr.end(), [](auto &a, auto &b){
  9. return a.second < b.second;
  10. });
  11.  
  12. int count = 1;
  13. int last = arr[0].second;
  14.  
  15. for (int i = 1; i < arr.size(); i++) {
  16. if (arr[i].first >= last) {
  17. count++;
  18. last = arr[i].second;
  19. }
  20. }
  21.  
  22. cout << "Max non-overlapping intervals: " << count << endl;
  23. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Max non-overlapping intervals: 3