Design Underground System LeetCode solution in C++

1
typedef pair<string, int> PSI;
2
typedef pair<string, string> PSS;
3
typedef vector<int> VI;
4
typedef vector<PSI> VPSI;
5
typedef map<PSS, VI> HPSSVI;
6
typedef map<int, VPSI> HIVPSI;
7

8
class UndergroundSystem {
9
HPSSVI schedule;
10
HIVPSI customer;
11

12
public:
13
UndergroundSystem() {}
14

15
void checkIn(int id, string stationName, int t) {
16
customer[id].push_back(make_pair(stationName, t));
17
}
18

19
void checkOut(int id, string stationName, int t) {
20
auto it = customer.find(id);
21
int idx = it->second.size() - 1;
22
schedule[{it->second[idx].first, stationName}].push_back(t - it->second[idx].second);
23
}
24

25
double getAverageTime(string startStation, string endStation) {
26
double sum = 0.0;
27
int s = schedule[{startStation, endStation}].size();
28
auto it = schedule.find({startStation, endStation});
29
for (int i = 0; i < s; i++) sum += schedule[{startStation, endStation}][i];
30
return sum / s;
31
}
32
};
33

34
/**
35
* Your UndergroundSystem object will be instantiated and called as such:
36
* UndergroundSystem* obj = new UndergroundSystem();
37
* obj->checkIn(id,stationName,t);
38
* obj->checkOut(id,stationName,t);
39
* double param_3 = obj->getAverageTime(startStation,endStation);
40
*/

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0