-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathDesign Underground System.cpp
More file actions
56 lines (44 loc) · 1.45 KB
/
Copy pathDesign Underground System.cpp
File metadata and controls
56 lines (44 loc) · 1.45 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Link to the " Design Underground System " Problem ==>> https://leetcode.com/problems/design-underground-system/
Link to the complete Explaination Video ==>> https://youtu.be/kr5up2nnDxM
*/
#define ll double
class UndergroundSystem {
unordered_map<string,vector<ll>> sum;
unordered_map<int,pair<string,ll>> entry;
public:
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
entry[id]={stationName,t};
}
void checkOut(int id, string stationName, int t) {
stationName += '-'+entry[id].first;
auto tr=sum.find(stationName);
if (tr == sum.end())
{
sum[stationName]={1,t-entry[id].second};
}
else
{
tr->second[0]++;
tr->second[1] += t-entry[id].second;
}
}
double getAverageTime(string startStation, string endStation) {
endStation += '-'+startStation;
auto tr=sum.find(endStation);
if (tr != sum.end())
{
return ((double) tr->second[1])/(double)tr->second[0];
}
else return 0;
}
};
/**
* 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);
*/