-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
calendar_rendering.cc
60 lines (52 loc) · 1.77 KB
/
calendar_rendering.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <algorithm>
#include <iterator>
#include <vector>
#include "test_framework/generic_test.h"
#include "test_framework/serialization_traits.h"
using std::max;
using std::vector;
struct Event {
int start, finish;
};
int FindMaxSimultaneousEvents(const vector<Event>& A) {
struct Endpoint {
int time;
bool is_start;
};
// Builds an array of all endpoints.
vector<Endpoint> E;
for (const Event& event : A) {
E.push_back({event.start, true});
E.push_back({event.finish, false});
}
// Sorts the endpoint array according to the time, breaking ties
// by putting start times before end times.
sort(begin(E), end(E), [](const Endpoint& a, const Endpoint& b) {
// If times are equal, an endpoint that starts an interval comes first.
return a.time != b.time ? a.time < b.time : (a.is_start && !b.is_start);
});
// Track the number of simultaneous events, and record the maximum
// number of simultaneous events.
int max_num_simultaneous_events = 0, num_simultaneous_events = 0;
for (const Endpoint& endpoint : E) {
if (endpoint.is_start) {
++num_simultaneous_events;
max_num_simultaneous_events =
max(num_simultaneous_events, max_num_simultaneous_events);
} else {
--num_simultaneous_events;
}
}
return max_num_simultaneous_events;
}
namespace test_framework {
template <>
struct SerializationTrait<Event> : UserSerTrait<Event, int, int> {};
} // namespace test_framework
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"A"};
return GenericTestMain(args, "calendar_rendering.cc",
"calendar_rendering.tsv", &FindMaxSimultaneousEvents,
DefaultComparator{}, param_names);
}