forked from Chayandas07/LeetCode-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1396. Design Underground System
42 lines (31 loc) · 1.19 KB
/
1396. Design Underground System
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
class UndergroundSystem {
public:
// id -> {station name,time}
unordered_map<int,pair<string,int>>checkInStation;
// Route -> {total time,count}
unordered_map<string,pair<int,int>> checkOutStation;
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
checkInStation[id] = {stationName,t};
}
void checkOut(int id, string stationName, int t) {
auto cIn = checkInStation[id];
checkInStation.erase(id);
string route = cIn.first + "_" + stationName;
checkOutStation[route].first += t - cIn.second;
checkOutStation[route].second += 1;
}
double getAverageTime(string startStation, string endStation) {
string route = startStation + "_" + endStation;
auto time = checkOutStation[route];
return (double)time.first/time.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);
*/