-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndergroundSystem.cpp
More file actions
36 lines (31 loc) · 1.15 KB
/
UndergroundSystem.cpp
File metadata and controls
36 lines (31 loc) · 1.15 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
class UndergroundSystem {
public:
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
ckin[id] = {stationName, t};
}
void checkOut(int id, string stationName, int t) {
string inStation = ckin[id].first;
int inTime = ckin[id].second;
if (!data.count(inStation) || !data[inStation].count(stationName))
data[inStation][stationName] = {1, t - inTime};
else {
++data[inStation][stationName].first;
data[inStation][stationName].second += t - inTime;
}
}
double getAverageTime(string startStation, string endStation) {
return 1.0 * data[startStation][endStation].second / data[startStation][endStation].first;
}
private:
unordered_map<int, pair<string, int> > ckin;
unordered_map<string, unordered_map<string, pair<int, int> > > data;
};
/**
* 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);
*/