-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesignUndergroundSystem.py
37 lines (29 loc) · 1.1 KB
/
designUndergroundSystem.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
31
32
33
34
35
36
37
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/design-underground-system/
# Author: Miao Zhang
# Date: 2021-04-29
class UndergroundSystem:
def __init__(self):
# id -> station, t
self.id = {}
# from_to: sums, cnt
self.stations = {}
def checkIn(self, id: int, stationName: str, t: int) -> None:
self.id[id] = [stationName, t]
def checkOut(self, id: int, stationName: str, t: int) -> None:
s0, t0 = self.id[id]
key = s0 + '_' + stationName
if key not in self.stations:
self.stations[key] = [(t - t0), 1]
else:
self.stations[key][0] += (t - t0)
self.stations[key][1] += 1
def getAverageTime(self, startStation: str, endStation: str) -> float:
sums, cnt = self.stations[startStation + "_" + endStation]
return sums / cnt
# 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)