AlgoPlus v0.1.0
Loading...
Searching...
No Matches
scheduling.h
1#ifndef SCHEDULING_H
2#define SCHEDULING_H
3
4#ifdef __cplusplus
5#include <algorithm>
6#include <iostream>
7#include <vector>
8#endif
9
17int scheduling(std::vector<std::pair<int, int>>& intervals) {
18 std::sort(intervals.begin(), intervals.end(),
19 [&](const std::pair<int, int>& a, const std::pair<int, int>& b) {
20 if (a.second == b.second) {
21 return a.first < b.first;
22 }
23 return a.second < b.second;
24 });
25
26 auto [start_x, start_y] = intervals[0];
27 int count_valid = 1;
28 for (int i = 1; i < int(intervals.size()); i++) {
29 auto [x, y] = intervals[i];
30 if (x >= start_y) {
31 count_valid++;
32 start_x = intervals[i].first;
33 start_y = intervals[i].second;
34 }
35 }
36
37 return count_valid;
38}
39
40#endif