-
Notifications
You must be signed in to change notification settings - Fork 0
/
1396. Design Underground System 2.cpp
47 lines (38 loc) · 1.34 KB
/
1396. Design Underground System 2.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
41
42
43
44
45
46
47
// https://leetcode.com/problems/design-underground-system/
class UndergroundSystem
{
unordered_map<int, string> place;
unordered_map<string, int> time;
unordered_map<string, pair<int, int>> tripInfo;
public:
UndergroundSystem()
{
}
void checkIn(int id, string stationName, int t)
{
place[id] = stationName;
string customerStation = to_string(id) + ":" + stationName;
time[customerStation] = t;
}
void checkOut(int id, string stationName, int t)
{
string startStation = place[id];
string combinedStation = startStation + ":" + stationName;
string customerStation = to_string(id) + ":" + startStation;
int takenTime = t - time[customerStation];
tripInfo[combinedStation].first += takenTime;
tripInfo[combinedStation].second++;
}
double getAverageTime(string startStation, string endStation)
{
string combinedStation = startStation + ":" + endStation;
return (double)tripInfo[combinedStation].first / (double)tripInfo[combinedStation].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);
*/