1396. Design Underground System
https://leetcode.com/problems/design-underground-system/
Implement the class UndergroundSystem
that supports three methods:
1. checkIn(int id, string stationName, int t)
A customer with id card equal to
id
, gets in the stationstationName
at timet
.A customer can only be checked into one place at a time.
2. checkOut(int id, string stationName, int t)
A customer with id card equal to
id
, gets out from the stationstationName
at timet
.
3. getAverageTime(string startStation, string endStation)
Returns the average time to travel between the
startStation
and theendStation
.The average time is computed from all the previous traveling from
startStation
toendStation
that happened directly.Call to
getAverageTime
is always valid.
You can assume all calls to checkIn
and checkOut
methods are consistent. That is, if a customer gets in at time t1 at some station, then it gets out at time t2 with t2 > t1. All events happen in chronological order.
Example 1:
Constraints:
There will be at most
20000
operations.1 <= id, t <= 10^6
All strings consist of uppercase, lowercase English letters and digits.
1 <= stationName.length <= 10
Answers within
10^-5
of the actual value will be accepted as correct.
设计简易进出站统计系统,分别实现:checkIn(t, s, u)表示用户u在t时刻进s站,checkOut(t, s, u)表示出站,getAvgTime(s1, s2)统计所有直接从s1到s2站的旅客平均花费时间。需要统计s1到s2的旅客平均时间,因此用dict s[s1][s2]记录从s1到s2的总时间和总人数。再用dict u记录用户何时进出何站。
Last updated
Was this helpful?