aboutsummaryrefslogtreecommitdiff
path: root/design_underground_system.cpp
blob: f0904d2e512e4b3d610778cef743a61a8ad7616c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
  }
};