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
-
void checkIn(int id, string stationName, int t) -
A customer with a card ID equal to
id , checks in at the stationstationName at timet . - 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 stationstationName at timet . -
double getAverageTime(string startStation, string endStation) -
Returns the average time it takes to travel from
startStation toendStation . -
The average time is computed from all the previous traveling times from
startStation toendStation that happened directly, meaning a check in atstartStation followed by a check out fromendStation . -
The time it takes to travel from
startStation toendStation may be different from the time it takes to travel fromendStation tostartStation . -
There will be at least one customer that has traveled from
startStation toendStation beforegetAverageTime is called.
You may assume all calls to the
"checkIn" : [45,"Leyton",3]
"checkIn" : [32,"Paradise",8]
"checkIn" : [27,"Leyton",10]
"checkOut" : [45,"Waterloo",15]
"checkOut" : [27,"Waterloo",20]
"checkOut" : [32,"Cambridge",22]
"getAverageTime" : ["Paradise","Cambridge"]
"getAverageTime" : ["Leyton","Waterloo"]
"checkIn" : [10,"Leyton",24]
"getAverageTime" : ["Leyton","Waterloo"]
"checkOut" : [10,"Waterloo",38]
"getAverageTime" : ["Leyton","Waterloo"]
Output:
Operation 1 "checkIn" : void
Operation 2 "checkIn" : void
Operation 3 "checkIn" : void
Operation 4 "checkOut" : void
Operation 5 "checkOut" : void
Operation 6 "checkOut" : void
Operation 7 "getAverageTime" : 14.00000
Operation 8 "getAverageTime" : 11.00000
Operation 9 "checkIn" : void
Operation 10 "getAverageTime" : 11.00000
Operation 11 "checkOut" : void
Operation 12 "getAverageTime" : 12.00000
Explanation:
undergroundSystem.checkIn(45, "Leyton", 3);
undergroundSystem.checkIn(32, "Paradise", 8);
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
Constraints:
1 <= id, t <= 106 - 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 * 104 calls in total tocheckIn ,checkOut , andgetAverageTime . - Answers within
10-5 of the actual value will be accepted.
Contents
Solution 1 - Store checkin and checkout time information into a HashMaps
In this approach, we will use two
Implementation steps:
-
We will create two
HashMap 's,checkInMap to store checkin information, andtotalTimeToCountMap to track total time took between stations, and the number of travels made between those stations, that reason we track this is to compute the average,average = total time / count . -
Implementation steps for
checkIn (id, stationName, t) :-
In
checkIn method, add this information intocheckInMap , withid as key and(stationName, t) pair as value.
-
In
-
Implementation steps for
checkOut (id, stationName, t) :-
When a person is checked out, we will get their checkin information from
checkInMap using the theirid , and then get the total time it took from starting station to end station by taking the diff betweent passed tocheckOut method and thet stored incheckinMap . We then store information intototalTimeToCountMap with(startStation, endStation) pair as key and(total time it took to travel, number of travels made between these stations) pair as value.
-
When a person is checked out, we will get their checkin information from
-
Implementation steps for
getAverageTime(String startStation, String endStation) :-
We will get total time and count information from
totalTimeToCountMap by passing(startStation, endStation) as key and simply computetotal time it took to travel /number of travels made between these stations that is already stored as value in the map.
-
We will get total time and count information from
Complexity Analysis:
Time complexity: Above code runs in O(1) since we are using
Space complexity: O(n), since we are storing information into
Above implementations source code can be found at