-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path20. Design Underground System
50 lines (42 loc) · 1.42 KB
/
20. Design Underground System
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
48
49
50
class UndergroundSystem {
Map<Integer, ArrivalInfo> arrivals;
Map<String, double[]> total;
public UndergroundSystem() {
arrivals = new HashMap<>();
total = new HashMap<>();
}
public void checkIn(int id, String stationName, int t) {
arrivals.put(id, new ArrivalInfo(id, stationName, t));
}
public void checkOut(int id, String stationName, int t) {
ArrivalInfo arrival = arrivals.get(id);
String key = arrival.stationName + "_" + stationName;
double[] pair = total.getOrDefault(key, new double[2]);
int time = t-arrival.time;
pair[0]+=time;
pair[1]++;
total.put(key,pair);
}
public double getAverageTime(String startStation, String endStation) {
String key = startStation + "_" + endStation;
double[] pair = total.get(key);
return pair[0]/pair[1];
}
class ArrivalInfo {
int id;
String stationName;
int time;
ArrivalInfo(int id, String stationName, int time){
this.id = id;
this.stationName = stationName;
this.time = 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);
*/