-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathDesign Underground System.java
More file actions
43 lines (36 loc) 路 1.75 KB
/
Copy pathDesign Underground System.java
File metadata and controls
43 lines (36 loc) 路 1.75 KB
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
38
39
40
41
42
43
class UndergroundSystem {
private final Map<Integer, StationTimePair> customerToCheckInMapping;
private final Map<String, Integer> startEndStationTotalTimeMapping;
private final Map<String, Integer> startEndStationCountMapping;
public UndergroundSystem() {
this.customerToCheckInMapping = new HashMap<>();
this.startEndStationTotalTimeMapping = new HashMap<>();
this.startEndStationCountMapping = new HashMap<>();
}
public void checkIn(int id, String stationName, int t) {
StationTimePair pair = new StationTimePair(stationName, t);
customerToCheckInMapping.put(id, pair);
}
public void checkOut(int id, String stationName, int t) {
StationTimePair pair = customerToCheckInMapping.get(id);
String key = pair.station() + "|" + stationName;
int totalTime = t - pair.time();
startEndStationTotalTimeMapping.put(key, startEndStationTotalTimeMapping.getOrDefault(key, 0) + totalTime);
startEndStationCountMapping.put(key, startEndStationCountMapping.getOrDefault(key, 0) + 1);
}
public double getAverageTime(String startStation, String endStation) {
String key = startStation + "|" + endStation;
if (!startEndStationTotalTimeMapping.containsKey(key)) {
return 0.0;
}
return ((double) startEndStationTotalTimeMapping.get(key)) / startEndStationCountMapping.get(key);
}
static record StationTimePair(String station, int time) {}
}
/**
* Your UndergroundSystem object will be instantiated and called as such:
* UndergroundSystem obj = new UndergroundSystem();
* obj.checkIn(id,stationName,t);
* obj.checkOut(id,stationName,t);
* double param_3 = obj.getAverageTime(startStation,endStation);
*/