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 &lt; t2. All events happen in chronological order.

&nbsp;
Example 1:

Input
["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
[[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]]

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(32, "Paradise", 8);
undergroundSystem.checkIn(27, "Leyton", 10);
undergroundSystem.checkOut(45, "Waterloo", 15);  // Customer 45 "Leyton" -&gt; "Waterloo" in 15-3 = 12
undergroundSystem.checkOut(27, "Waterloo", 20);  // Customer 27 "Leyton" -&gt; "Waterloo" in 20-10 = 10
undergroundSystem.checkOut(32, "Cambridge", 22); // Customer 32 "Paradise" -&gt; "Cambridge" in 22-8 = 14
undergroundSystem.getAverageTime("Paradise", "Cambridge"); // return 14.00000. One trip "Paradise" -&gt; "Cambridge", (14) / 1 = 14
undergroundSystem.getAverageTime("Leyton", "Waterloo");    // return 11.00000. Two trips "Leyton" -&gt; "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" -&gt; "Waterloo" in 38-24 = 14
undergroundSystem.getAverageTime("Leyton", "Waterloo");    // return 12.00000. Three trips "Leyton" -&gt; "Waterloo", (10 + 12 + 14) / 3 = 12


Example 2:

Input
["UndergroundSystem","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime"]
[[],[10,"Leyton",3],[10,"Paradise",8],["Leyton","Paradise"],[5,"Leyton",10],[5,"Paradise",16],["Leyton","Paradise"],[2,"Leyton",21],[2,"Paradise",30],["Leyton","Paradise"]]

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" -&gt; "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" -&gt; "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" -&gt; "Paradise" in 30-21 = 9
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 6.66667, (5 + 6 + 9) / 3 = 6.66667


&nbsp;
Constraints:


	1 &lt;= id, t &lt;= 106
	1 &lt;= stationName.length, startStation.length, endStation.length &lt;= 10
	All strings consist of uppercase and lowercase English letters and digits.
	There will be at most 2 * 104 calls in total to checkIn, checkOut, and getAverageTime.
	Answers within 10-5 of the actual value will be accepted.


from collections import defaultdict
class UndergroundSystem:
    def __init__(self):
        self.checkin = {}
        self.traveltime = defaultdict(dict)

    def checkIn(self, id: int, stationName: str, t: int) -> None:
        self.checkin[id] = (stationName, t)

    def checkOut(self, id: int, stationName: str, t: int) -> None:
        startStation, startTime = self.checkin[id]
        del self.checkin[id]
        if stationName not in self.traveltime[startStation]:
            self.traveltime[startStation][stationName] = []
        self.traveltime[startStation][stationName].append(t-startTime)

    def getAverageTime(self, startStation: str, endStation: str) -> float:
        return sum(self.traveltime[startStation][endStation])/len(self.traveltime[startStation][endStation])
class UndergroundSystem {
    HashMap<Integer, Travel> travelMap;
    HashMap<String, Averages> avgMap;
    
    public UndergroundSystem() {
        travelMap = new HashMap();
        avgMap = new HashMap();
    }
    
    public void checkIn(int id, String stationName, int t) {
        Travel journey = new Travel(id, t, stationName);
        travelMap.put(id, journey);
    }
    
    public void checkOut(int id, String stationName, int t) {
        Travel journey = travelMap.get(id);
        
        int journeyTime = t - journey.checkIn;
        String key = journey.startStation + "," + stationName;
        
        Averages average = avgMap.containsKey(key) ? avgMap.get(key) : new Averages();
        average.updateAverage(journeyTime);
        
        avgMap.put(key, average);
    }
    
    public double getAverageTime(String startStation, String endStation) {
        String key = startStation + "," + endStation;
        Averages average = avgMap.get(key);
        
        int totalTrips = average.totalTrips;
        double totalJourneytime = average.totalTraveltime;
        double averageTime = totalJourneytime/totalTrips;
        
        return averageTime;
    }
    
    class Travel{
        int id;
        String startStation;
        int checkIn;
        
        public Travel(int id, int checkIn, String startStation)
        {
            this.id = id;
            this.checkIn = checkIn;
            this.startStation = startStation;
        }
    }
    
    class Averages{
        double totalTraveltime;
        int totalTrips;
        
        public Averages()
        {
            totalTraveltime = 0;
            totalTrips = 0;
        }
        
        private void updateAverage(int journeyTime)
        {
            totalTraveltime += journeyTime;
            totalTrips++;
        }
    }
}
typedef pair<string, int> PSI;
typedef pair<string, string> PSS;
typedef vector<int> VI;
typedef vector<PSI> VPSI;
typedef map<PSS, VI> HPSSVI;
typedef map<int, VPSI> HIVPSI;

class UndergroundSystem {
    HPSSVI schedule;
    HIVPSI customer;
public:
    UndergroundSystem() {
        
    }
    
    void checkIn(int id, string stationName, int t) {
        customer[id].push_back(make_pair(stationName, t));
    }
    
    void checkOut(int id, string stationName, int t) {
        auto it=customer.find(id);
        int idx = it->second.size()-1;
        schedule[{it->second[idx].first, stationName}].push_back(t - it->second[idx].second);
    }
    
    double getAverageTime(string startStation, string endStation) {
        double sum=0.0;
        int s=schedule[{startStation,endStation}].size();
        auto it=schedule.find({startStation,endStation});
        for(int i=0;i<s;i++)
            sum += schedule[{startStation,endStation}][i];
        return sum/s;
    }
};

/**
 * Your UndergroundSystem object will be instantiated and called as such:
 * UndergroundSystem* obj = new UndergroundSystem();
 * obj->checkIn(id,stationName,t);
 * obj->checkOut(id,stationName,t);
 * double param_3 = obj->getAverageTime(startStation,endStation);
 */
var UndergroundSystem = function() {
    this.checkInMap = {}
    this.tripMap = {}
};

/** 
 * @param {number} id 
 * @param {string} stationName 
 * @param {number} t
 * @return {void}
 */
UndergroundSystem.prototype.checkIn = function(id, stationName, t) {
    this.checkInMap[id] = [stationName, t]
};

/** 
 * @param {number} id 
 * @param {string} stationName 
 * @param {number} t
 * @return {void}
 */
UndergroundSystem.prototype.checkOut = function(id, stationName, t) {
    const [checkInStationName, checkInTime] = this.checkInMap[id]
    const tripKey = `${checkInStationName}-${stationName}`
    
    if (!this.tripMap[tripKey]) this.tripMap[tripKey] = [0,0]
    
    this.tripMap[tripKey][0] += t - checkInTime
    this.tripMap[tripKey][1]++
};

/** 
 * @param {string} startStation 
 * @param {string} endStation
 * @return {number}
 */
UndergroundSystem.prototype.getAverageTime = function(startStation, endStation) {
    const tripkey = `${startStation}-${endStation}`
    const [totalTripTime, totalTrips] = this.tripMap[tripkey]
    return totalTripTime / totalTrips
};
Design Underground System