forked from srishilesh/Data-Structure-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1396_Design_underground_system
27 lines (20 loc) · 985 Bytes
/
1396_Design_underground_system
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// https://leetcode.com/problems/design-underground-system/
class UndergroundSystem:
def __init__(self):
self.times = defaultdict(lambda: defaultdict(lambda : [0, 0]))
self.transit = defaultdict(list)
def checkIn(self, id: int, stationName: str, t: int) -> None:
self.transit[id].extend([stationName, t])
def checkOut(self, id: int, stationName: str, t: int) -> None:
sName, sTime = self.transit[id]
count, val = self.times[sName][stationName]
self.times[sName][stationName] = [count + 1, val + t - sTime]
self.transit[id] = []
def getAverageTime(self, startStation: str, endStation: str) -> float:
count, val = self.times[startStation][endStation]
return val / count
# 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)