-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1396_Design-Underground-System.cpp
40 lines (32 loc) · 1.21 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
40
class UndergroundSystem {
public:
unordered_map<int, pair<string, int>> record;
unordered_map<string, unordered_map<string, pair<int,int>>> time_collection;
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
record[id] = {stationName, t};
}
void checkOut(int id, string stationName, int t) {
int travelTime = t - record[id].second;
string startStation = record[id].first;
record.erase(id);
auto &p = time_collection[startStation][stationName];
p.first += travelTime;
p.second += 1;
}
double getAverageTime(string startStation, string endStation) {
if(time_collection[startStation].find(endStation) == time_collection[startStation].end()){
return 0.0;
}
auto &data = time_collection[startStation][endStation];
return static_cast<double>(data.first) / (data.second);
}
};
/**
* Your UndergroundSystem object will be instantiated and called as such:
* UndergroundSystem* obj = new UndergroundSystem();
* obj->checkIn(id,stationName,t);
* obj->checkOut(id,stationName,t);
* double param_3 = obj->getAverageTime(startStation,endStation);
*/