-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesignUndergroundSystem.java
More file actions
47 lines (40 loc) · 1.49 KB
/
DesignUndergroundSystem.java
File metadata and controls
47 lines (40 loc) · 1.49 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
44
45
46
47
class UndergroundSystem {
// Id to startStation and startTime
HashMap<Integer, Pair<String, Integer>> customersStartMap;
// startStation-endStation to Average
HashMap<String, Average> stationsPairMap;
public UndergroundSystem() {
this.customersStartMap = new HashMap<>();
this.stationsPairMap = new HashMap<>();
}
public void checkIn(int id, String stationName, int t) {
customersStartMap.put(id, new Pair(stationName, t));
}
public void checkOut(int id, String stationName, int t) {
Pair<String, Integer> customerStart = customersStartMap.get(id);
String key = customerStart.getKey()+"-"+stationName;
stationsPairMap.computeIfAbsent(key, (k) -> new Average()).add(t-customerStart.getValue());
}
public double getAverageTime(String startStation, String endStation) {
String key = startStation+"-"+endStation;
return stationsPairMap.get(key).getAverage();
}
private class Average {
private int n;
private long totalTime;
public double getAverage() {
return totalTime/(n+0d);
}
public void add(int time) {
n++;
totalTime += 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);
*/