-
Notifications
You must be signed in to change notification settings - Fork 0
/
1396. Design Underground System.cpp
39 lines (34 loc) · 1.1 KB
/
1396. Design Underground System.cpp
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
class UndergroundSystem {
public:
unordered_map<string, pair<long long, int>> Times;
unordered_map<int, pair<string,int>> Transit;
UndergroundSystem() {
Times.clear();
Transit.clear();
}
void checkIn(int id, string sName, int t) {
if(Transit.find(id) != Transit.end()) return;
Transit[id] = {sName, t};
}
void checkOut(int id, string sName, int t) {
auto& info = Transit[id];
string startStation = info.first;
int Start = info.second;
string key = startStation + ":" + sName;
int time = t - Start;
if(Times.find(key) != Times.end()) {
auto& oldTimes = Times[key];
oldTimes.first += time;
oldTimes.second++;
} else {
Times[key] = {time, 1};
}
Transit.erase(id);
}
double getAverageTime(string startStation, string endStation) {
string key = startStation + ":" + endStation;
auto & info = Times[key];
double avg = (double)info.first / (double)info.second;
return avg;
}
};