|
| 1 | +class UndergroundSystem { |
| 2 | + // id => entryStation |
| 3 | + string checkInName[1000001]; |
| 4 | + // id => entryTime |
| 5 | + int checkInTime[1000001]; |
| 6 | + // id => {numberOfTravels, overallTime} |
| 7 | + unordered_map<string, pair<int, int>> avgTimesMap; |
| 8 | +public: |
| 9 | + UndergroundSystem() { |
| 10 | + // cout << "I am a constructor and I am as useless as \"ueue\" in \"queue\"\n"; |
| 11 | + } |
| 12 | + |
| 13 | + void checkIn(int id, string stationName, int t) { |
| 14 | + checkInName[id] = stationName; |
| 15 | + checkInTime[id] = -t; |
| 16 | + } |
| 17 | + |
| 18 | + void checkOut(int id, string stationName, int t) { |
| 19 | + // retrieving matching checkin data |
| 20 | + int travelTime = checkInTime[id] + t; |
| 21 | + // computing travelName id |
| 22 | + string travelName = checkInName[id] + stationName; |
| 23 | + // updating data on the current travel |
| 24 | + auto currTravel = avgTimesMap.find(travelName); |
| 25 | + if (currTravel == end(avgTimesMap)) avgTimesMap[travelName] = {1, travelTime}; |
| 26 | + else { |
| 27 | + currTravel->second.first++; |
| 28 | + currTravel->second.second += travelTime; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + double getAverageTime(string startStation, string endStation) { |
| 33 | + // computing travelName id |
| 34 | + string travelName = startStation + endStation; |
| 35 | + // retrieving data on current travel |
| 36 | + auto currTravel = avgTimesMap[travelName]; |
| 37 | + // returning the average time |
| 38 | + return currTravel.second / (double)currTravel.first; |
| 39 | + } |
| 40 | +}; |
0 commit comments