diff options
Diffstat (limited to 'other/design_underground_system.cpp')
-rw-r--r-- | other/design_underground_system.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/other/design_underground_system.cpp b/other/design_underground_system.cpp new file mode 100644 index 0000000..f0904d2 --- /dev/null +++ b/other/design_underground_system.cpp @@ -0,0 +1,20 @@ +class UndergroundSystem { +public: + unordered_map<int, pair<string, int>> checkIns; + unordered_map<string, pair<int, int>> routes; + UndergroundSystem() {} + void checkIn(int id, string stationName, int t) { + checkIns[id] = {stationName, t}; + } + void checkOut(int id, string stationName, int t) { + auto [stn, start] = checkIns[id]; + checkIns.erase(id); + string route = stn + "," + stationName; + routes[route].first++; + routes[route].second += t - start; + } + double getAverageTime(string startStation, string endStation) { + auto &[count, sum] = routes[startStation + "," + endStation]; + return (double)sum / count; + } +}; |