Follow

Follow

Nhut Nguyen
Â·May 2, 2022Â·

Problem statement

An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another.

Implement the `UndergroundSystem` class:

• `void checkIn(int id, string stationName, int t)`
• A customer with a card ID equal to `id`, checks in at the station `stationName` at time `t`.
• A customer can only be checked into one place at a time.
• `void checkOut(int id, string stationName, int t)`
• A customer with a card ID equal to `id`, checks out from the station `stationName` at time `t`.
• `double getAverageTime(string startStation, string endStation)`
• Returns the average time it takes to travel from `startStation` to `endStation`.
• The average time is computed from all the previous traveling times from `startStation` to `endStation` that happened directly, meaning a check-in at `startStation` followed by a check-out from `endStation`.
• The time it takes to travel from `startStation` to `endStation` may be different from the time it takes to travel from `endStation` to `startStation`.
• There will be at least one customer that has traveled from `startStation` to `endStation` before `getAverageTime` is called.

You may assume all calls to the `checkIn` and `checkOut` methods are consistent. If a customer checks in at time `t1` then checks out at time `t2`, then `t1 < t2`. All events happen in chronological order.

Example 1

``````Input
["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]

Output
[null,null,null,null,null,null,null,14.00000,11.00000,null,11.00000,null,12.00000]

Explanation
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(45, "Leyton", 3);
undergroundSystem.checkIn(27, "Leyton", 10);
undergroundSystem.checkOut(45, "Waterloo", 15);  // Customer 45 "Leyton" -> "Waterloo" in 15-3 = 12
undergroundSystem.checkOut(27, "Waterloo", 20);  // Customer 27 "Leyton" -> "Waterloo" in 20-10 = 10
undergroundSystem.checkOut(32, "Cambridge", 22); // Customer 32 "Paradise" -> "Cambridge" in 22-8 = 14
undergroundSystem.getAverageTime("Paradise", "Cambridge"); // return 14.00000. One trip "Paradise" -> "Cambridge", (14) / 1 = 14
undergroundSystem.getAverageTime("Leyton", "Waterloo");    // return 11.00000. Two trips "Leyton" -> "Waterloo", (10 + 12) / 2 = 11
undergroundSystem.checkIn(10, "Leyton", 24);
undergroundSystem.getAverageTime("Leyton", "Waterloo");    // return 11.00000
undergroundSystem.checkOut(10, "Waterloo", 38);  // Customer 10 "Leyton" -> "Waterloo" in 38-24 = 14
undergroundSystem.getAverageTime("Leyton", "Waterloo");    // return 12.00000. Three trips "Leyton" -> "Waterloo", (10 + 12 + 14) / 3 = 12
``````

Example 2

``````Input
["UndergroundSystem","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime"]

Output
[null,null,null,5.00000,null,null,5.50000,null,null,6.66667]

Explanation
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(10, "Leyton", 3);
undergroundSystem.checkOut(10, "Paradise", 8); // Customer 10 "Leyton" -> "Paradise" in 8-3 = 5
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.00000, (5) / 1 = 5
undergroundSystem.checkIn(5, "Leyton", 10);
undergroundSystem.checkOut(5, "Paradise", 16); // Customer 5 "Leyton" -> "Paradise" in 16-10 = 6
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.50000, (5 + 6) / 2 = 5.5
undergroundSystem.checkIn(2, "Leyton", 21);
undergroundSystem.checkOut(2, "Paradise", 30); // Customer 2 "Leyton" -> "Paradise" in 30-21 = 9
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 6.66667, (5 + 6 + 9) / 3 = 6.66667
``````

Constraints

• `1 <= id, t <= 10^6`.
• `1 <= stationName.length, startStation.length, endStation.length <= 10`.
• All strings consist of uppercase and lowercase English letters and digits.
• There will be at most `2 * 10^4` calls in total to `checkIn`, `checkOut`, and `getAverageTime`.
• Answers within `1e-5` of the actual value will be accepted.

Solution 1: Store all travel times between the stations

Code

``````#include <iostream>
#include <unordered_map>
#include <numeric>
#include <vector>
using namespace std;

class UndergroundSystem {
unordered_map< string, vector<int> > _routes;
unordered_map< int, pair<string, int> > _checkIns;
public:
UndergroundSystem() {

}
void checkIn(int id, string stationName, int t) {
_checkIns[id] = make_pair(stationName, t);
}
void checkOut(int id, string stationName, int t) {
const string route = _checkIns[id].first + "-" + stationName;
_routes[route].push_back(t - _checkIns[id].second);
}
double getAverageTime(string startStation, string endStation) {
const string route = startStation + "-" + endStation;
vector<int>& v = _routes[route];
return (double) accumulate(v.begin(), v.end(), 0) / v.size();
}
};
int main() {
UndergroundSystem u;
u.checkIn(45, "Leyton", 3);
u.checkIn(27, "Leyton", 10);
u.checkOut(45, "Waterloo", 15);
u.checkOut(27, "Waterloo", 20);
u.checkOut(32, "Cambridge", 22);
cout << u.getAverageTime("Paradise", "Cambridge") << endl;
cout << u.getAverageTime("Leyton", "Waterloo") << endl;
u.checkIn(10, "Leyton", 24);
cout << u.getAverageTime("Leyton", "Waterloo") << endl;
u.checkOut(10, "Waterloo", 38);
cout << u.getAverageTime("Leyton", "Waterloo") << endl;
}
``````
``````Output:
14
11
11
12
``````

Complexity

• Runtime: `O(1)`.
• Extra space: `O(2*N + 2^M * P)`, where `N` is the number of customers, `M` is the number of stations and `P` is the average number of travels between the stations.

Solution 2: Store only the total travel time

Code

``````#include <iostream>
#include <unordered_map>
using namespace std;

class UndergroundSystem {
unordered_map< string, pair<int, int> > _routes;
unordered_map< int, pair<string, int> > _checkIns;
public:
UndergroundSystem() {

}
void checkIn(int id, string stationName, int t) {
_checkIns[id] = {stationName, t};
}
void checkOut(int id, string stationName, int t) {
const string route = _checkIns[id].first + "-" + stationName;
auto& p = _routes[route];
p.first += t - _checkIns[id].second;
p.second++;
}
double getAverageTime(string startStation, string endStation) {
const string route = startStation + "-" + endStation;
auto& p = _routes[route];
return (double) p.first / p.second;
}
};
int main() {
UndergroundSystem u;
u.checkIn(45, "Leyton", 3);
u.checkIn(27, "Leyton", 10);
u.checkOut(45, "Waterloo", 15);
u.checkOut(27, "Waterloo", 20);
u.checkOut(32, "Cambridge", 22);
cout << u.getAverageTime("Paradise", "Cambridge") << endl;
cout << u.getAverageTime("Leyton", "Waterloo") << endl;
u.checkIn(10, "Leyton", 24);
cout << u.getAverageTime("Leyton", "Waterloo") << endl;
u.checkOut(10, "Waterloo", 38);
cout << u.getAverageTime("Leyton", "Waterloo") << endl;
}
``````
``````Output:
14
11
11
12
``````

Complexity

• Runtime: `O(1)`.
• Extra space: `O(2*N + 2^M * P)`, where `N` is the number of customers, `M` is the number of stations and `P` is the average number of travels between the stations.