-
Notifications
You must be signed in to change notification settings - Fork 4
/
design-underground-system.cpp
40 lines (34 loc) · 1.31 KB
/
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:
//hash-map to store the checkin details
//map<startstation,time>
map<int,pair<string,int>> in;
//hashmap to store the total sum and count of travel between two stations
//map<<endstation,startstation><totaltime(sum),count>>
map<pair<string,string>,pair<int,int>> avg;
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
//update the checkin
in[id]={stationName,t};
}
void checkOut(int id, string stationName, int t) {
//update the total time(sum) and count of travel
avg[{stationName,
in[id].first}]={avg[{stationName,
in[id].first}].first + t- in[id].second,
avg[{stationName,
in[id].first}].second + 1};
}
double getAverageTime(string startStation, string endStation) {
//return the average
return double(double(avg[{endStation,startStation}].first)/double(avg[{endStation,startStation}].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);
*/