1
+ class UndergroundSystem (object ):
2
+
3
+ def __init__ (self ):
4
+ self .check_in_history = dict () # key is id + stationName, val is t
5
+ self .StationTraverlTime = dict ()
6
+ def checkIn (self , id , stationName , t ):
7
+ """
8
+ :type id: int
9
+ :type stationName: str
10
+ :type t: int
11
+ :rtype: None
12
+ """
13
+ self .check_in_history [str (id )] = [stationName , t ]
14
+
15
+
16
+ def checkOut (self , id , stationName , t ):
17
+ """
18
+ :type id: int
19
+ :type stationName: str
20
+ :type t: int
21
+ :rtype: None
22
+ """
23
+ start_station , start_time = self .check_in_history [str (id )]
24
+ self .check_in_history .pop (str (id ))
25
+ time_spent = t - start_time
26
+ key = start_station + "#" + stationName
27
+ if key not in self .StationTraverlTime :
28
+ self .StationTraverlTime [key ] = [time_spent ,1 ]
29
+ else :
30
+ self .StationTraverlTime [key ][0 ] += time_spent
31
+ self .StationTraverlTime [key ][1 ] += 1
32
+
33
+
34
+ def getAverageTime (self , startStation , endStation ):
35
+ """
36
+ :type startStation: str
37
+ :type endStation: str
38
+ :rtype: float
39
+ """
40
+ key = startStation + "#" + endStation
41
+ return self .StationTraverlTime [key ][0 ] * 1.0 / self .StationTraverlTime [key ][1 ]
42
+
43
+
44
+ # Your UndergroundSystem object will be instantiated and called as such:
45
+ # obj = UndergroundSystem()
46
+ # obj.checkIn(id,stationName,t)
47
+ # obj.checkOut(id,stationName,t)
48
+ # param_3 = obj.getAverageTime(startStation,endStation)
0 commit comments