Implement UndergroundSystem to track completed railway trips.
UndergroundSystem()initializes the system.checkIn(id, stationName, t)records a customer entering a station at timet. A customer cannot be checked in twice at the same time.checkOut(id, stationName, t)completes that customer's current trip. Its duration is the checkout time minus the check-in time.getAverageTime(startStation, endStation)returns the average duration of all completed trips with exactly that start and end station.
Routes are directional: trips from A to B are tracked separately from trips from B to A. In-progress trips do not affect averages. Events arrive in chronological order, and every average query refers to a route with at least one completed trip. Answers with absolute error at most 0.00001 are accepted.
Example 1:
Input: ["UndergroundSystem","checkIn","checkOut","checkIn","checkOut","getAverageTime"]
[[],[1,"North",2],[1,"South",8],[2,"North",10],[2,"South",19],["North","South"]]
Output: [null,null,null,null,null,7.5]Explanation: The two completed North-to-South trips take 8 - 2 = 6 and 19 - 10 = 9 time units. Their average duration is (6 + 9) / 2 = 7.5.
Example 2:
Input: ["UndergroundSystem","checkIn","checkOut","checkIn","checkOut","getAverageTime","getAverageTime"]
[[],[1,"A",1],[1,"B",6],[1,"B",7],[1,"A",15],["A","B"],["B","A"]]
Output: [null,null,null,null,null,5.0,8.0]Explanation: The A-to-B trip takes 5 time units, while the later B-to-A trip takes 8. Opposite directions are separate routes, so the two queries return 5 and 8.
Example 3:
Input: ["UndergroundSystem","checkIn","checkOut","checkIn","getAverageTime"]
[[],[5,"X",3],[5,"Y",9],[5,"X",11],["X","Y"]]
Output: [null,null,null,null,6.0]Explanation: The completed X-to-Y trip takes 6 time units. Customer 5 checks in again, but that new trip is still in progress, so the average remains 6.
Constraints:
1 <= id, t <= 1000000- Station names contain 1 to 10 uppercase letters, lowercase letters, or digits.
- At most
20000calls are made to the three methods combined. - Events arrive in chronological order. Every checkout follows its matching check-in at a strictly later time.
- Customers have at most one active check-in, and every average query has at least one completed matching trip.
- Average answers are accepted within an absolute error of
0.00001.