Skip to content

Commit 5d87ad6

Browse files
authored
Create design-underground-system.py
1 parent d524abf commit 5d87ad6

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
import collections
5+
6+
7+
class UndergroundSystem(object):
8+
9+
def __init__(self):
10+
self.__live = {}
11+
self.__statistics = collections.defaultdict(lambda: [0, 0])
12+
13+
14+
def checkIn(self, id, stationName, t):
15+
"""
16+
:type id: int
17+
:type stationName: str
18+
:type t: int
19+
:rtype: None
20+
"""
21+
self.__live[id] = (stationName, t)
22+
23+
def checkOut(self, id, stationName, t):
24+
"""
25+
:type id: int
26+
:type stationName: str
27+
:type t: int
28+
:rtype: None
29+
"""
30+
startStation, startTime = self.__live.pop(id)
31+
self.__statistics[startStation, stationName][0] += t-startTime
32+
self.__statistics[startStation, stationName][1] += 1
33+
34+
def getAverageTime(self, startStation, endStation):
35+
"""
36+
:type startStation: str
37+
:type endStation: str
38+
:rtype: float
39+
"""
40+
numer, denom = self.__statistics[startStation, endStation]
41+
return float(numer) / denom

0 commit comments

Comments
 (0)