-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDesign Underground System
52 lines (45 loc) · 1.7 KB
/
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
51
52
class UndergroundSystem {
private Map<String, Map<Integer, Integer>> checkIn;
private Map<String, Map<Integer, Integer>> checkOut;
public UndergroundSystem() {
checkIn = new HashMap<>();
checkOut = new HashMap<>();
}
public void checkIn(int id, String stationName, int t) {
if( !checkIn.containsKey( stationName)) {
checkIn.put(stationName, new HashMap<Integer, Integer>());
}
Map<Integer, Integer> map = checkIn.get(stationName);
map.put(id, t);
}
public void checkOut(int id, String stationName, int t) {
if( !checkOut.containsKey( stationName)) {
checkOut.put(stationName, new HashMap<Integer, Integer>());
}
Map<Integer, Integer> map = checkOut.get(stationName);
map.put(id, t);
}
public double getAverageTime(String startStation, String endStation) {
double result =0.0;
Map<Integer, Integer> map1 = checkIn.get(startStation);
Map<Integer, Integer> map2 = checkOut.get(endStation);
int count =0;
for(Map.Entry<Integer, Integer> entry : map1.entrySet()) {
int startTime = entry.getValue();
int id = entry.getKey();
if( map2.containsKey(id)) {
int endTime = map2.get(id);
result += endTime - startTime;
count++;
}
}
return result/count;
}
}
/**
* 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);
*/