-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.py
30 lines (25 loc) · 1.06 KB
/
Solution.py
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
28
29
30
class UndergroundSystem:
def __init__(self):
self.check_in_station = {}
self.check_in_time = {}
self.total_time = {}
def checkIn(self, id: int, stationName: str, t: int) -> None:
self.check_in_station[id] = stationName
self.check_in_time[id] = t
def checkOut(self, id: int, stationName: str, t: int) -> None:
cost = t - self.check_in_time.pop(id)
start_station = self.check_in_station.pop(id)
stations = start_station + '.' + stationName
times = self.total_time.get(stations, [0, 0])
times[0] += cost
times[1] += 1
self.total_time[stations] = times
def getAverageTime(self, startStation: str, endStation: str) -> float:
stations = startStation + '.' + endStation
times = self.total_time[stations]
return times[0] / times[1]
# 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)