1396. Design Underground System
Problem
Design UndergroundSystem: checkIn(id, station, t) and checkOut(id, station, t) record a traveller's trip, and getAverageTime(start, end) returns the average duration of all completed trips that went directly from start to end (at least one exists). A traveller is checked into at most one station at a time.
Example: trips Leyton → Waterloo taking 12 and 10 → getAverageTime("Leyton","Waterloo") = 11.0
checkIn stores id → (station, time); checkOut pops it and accumulates (start, end) → [total time, trips].
Complexity: O(1) per operation.
class UndergroundSystem:
def __init__(self):
self.active, self.stats = {}, defaultdict(lambda: [0, 0])
def checkIn(self, id: int, stationName: str, t: int) -> None:
self.active[id] = (stationName, t)
def checkOut(self, id: int, stationName: str, t: int) -> None:
start, t0 = self.active.pop(id)
s = self.stats[(start, stationName)]
s[0] += t - t0
s[1] += 1
def getAverageTime(self, startStation: str, endStation: str) -> float:
total, n = self.stats[(startStation, endStation)]
return total / n
u = UndergroundSystem()
u.checkIn(45, "Leyton", 3); u.checkIn(32, "Paradise", 8); u.checkIn(27, "Leyton", 10)
u.checkOut(45, "Waterloo", 15); u.checkOut(27, "Waterloo", 20); u.checkOut(32, "Cambridge", 22)
assert u.getAverageTime("Paradise", "Cambridge") == 14.0 and u.getAverageTime("Leyton", "Waterloo") == 11.0
u2 = UndergroundSystem()
u2.checkIn(10, "A", 1); u2.checkOut(10, "B", 5); u2.checkIn(11, "A", 2); u2.checkOut(11, "B", 4)
assert u2.getAverageTime("A", "B") == 3.0
class UndergroundSystem {
private final Map<Integer, Object[]> active = new HashMap<>(); // id -> {station, time}
private final Map<String, long[]> stats = new HashMap<>(); // "a->b" -> {total, trips}
public void checkIn(int id, String stationName, int t) { active.put(id, new Object[]{stationName, t}); }
public void checkOut(int id, String stationName, int t) {
Object[] in = active.remove(id);
long[] s = stats.computeIfAbsent(in[0] + "->" + stationName, k -> new long[2]);
s[0] += t - (int) in[1];
s[1]++;
}
public double getAverageTime(String startStation, String endStation) {
long[] s = stats.get(startStation + "->" + endStation);
return (double) s[0] / s[1];
}
}
type checkIn struct {
station string
t int
}
type UndergroundSystem struct {
active map[int]checkIn // id -> where and when they entered
stats map[[2]string][2]int // (from, to) -> (total time, trips)
}
func Constructor() UndergroundSystem {
return UndergroundSystem{active: map[int]checkIn{}, stats: map[[2]string][2]int{}}
}
func (this *UndergroundSystem) CheckIn(id int, stationName string, t int) {
this.active[id] = checkIn{stationName, t}
}
func (this *UndergroundSystem) CheckOut(id int, stationName string, t int) {
in := this.active[id]
delete(this.active, id)
key := [2]string{in.station, stationName}
s := this.stats[key]
s[0] += t - in.t
s[1]++
this.stats[key] = s
}
func (this *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
s := this.stats[[2]string{startStation, endStation}]
return float64(s[0]) / float64(s[1])
}