1396. Design Underground System
https://leetcode.com/problems/design-underground-system/
Python
from collections import defaultdict
class UndergroundSystem:
def __init__(self):
self.traveling = dict()
self.mapper = defaultdict(list)
def checkIn(self, id: int, stationName: str, t: int) -> None:
if id in self.traveling:
# Duplicate check-in
return
self.traveling[id] = (stationName, t)
def checkOut(self, id: int, stationName: str, t: int) -> None:
if id not in self.traveling:
# Traveler not checked-in
return
in_station, in_t = self.traveling.pop(id)
out_station, out_t = stationName, t
self.mapper[(in_station, out_station)].append(out_t - in_t)
def getAverageTime(self, startStation: str, endStation: str) -> float:
if (startStation, endStation) not in self.mapper:
# No data
return
time_list = self.mapper[(startStation, endStation)]
return sum(time_list) / len(time_list)
# Your UndergroundSystem object will be instantiated and called as such:
# obj = UndergroundSystem()
# obj.checkIn(id,stationName,t)
# obj.checkOut(id,stationName,t)
# param_3 = obj.getAverageTime(startStation,endStation)