-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3885_Design-Event-Manager.cpp
More file actions
50 lines (45 loc) · 1.46 KB
/
3885_Design-Event-Manager.cpp
File metadata and controls
50 lines (45 loc) · 1.46 KB
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
class EventManager {
public:
struct cmp{
bool operator()(const pair<int,int>& a, const pair<int,int>& b){
// Pick smallest event ID for same priority
// Pick highest priority for different priority
if(a.first == b.first) return a.second > b.second;
return a.first < b.first;
}
};
priority_queue<pair<int,int>, vector<pair<int,int>>, cmp> maxHeap;
unordered_map<int, int> priority;
EventManager(vector<vector<int>>& events) {
for(auto& event : events){
int eventId = event[0];
int currPriority = event[1];
priority[eventId] = currPriority;
maxHeap.push({currPriority, eventId});
}
}
void updatePriority(int eventId, int newPriority) {
priority[eventId] = newPriority;
maxHeap.push({newPriority, eventId});
}
int pollHighest() {
// lazy
while(!maxHeap.empty()){
auto top = maxHeap.top();
maxHeap.pop();
int eventId = top.second;
int pri = top.first;
if(priority[eventId] == pri) {
priority.erase(eventId);
return eventId;
}
}
return -1;
}
};
/**
* Your EventManager object will be instantiated and called as such:
* EventManager* obj = new EventManager(events);
* obj->updatePriority(eventId,newPriority);
* int param_2 = obj->pollHighest();
*/