-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1396. Design Underground System.java
More file actions
45 lines (37 loc) · 1.24 KB
/
Copy path1396. Design Underground System.java
File metadata and controls
45 lines (37 loc) · 1.24 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
LeetCode
1396. Design Underground System
class CheckIn {
public String stationName;
public int time;
public CheckIn(String stationName, int time) {
this.stationName = stationName;
this.time = time;
}
}
class CheckOut {
public int numTrips;
public int totalTime;
public CheckOut(int numTrips, int totalTime) {
this.numTrips = numTrips;
this.totalTime = totalTime;
}
}
class UndergroundSystem {
public void checkIn(int id, String stationName, int t) {
checkIns.put(id, new CheckIn(stationName, t));
}
public void checkOut(int id, String stationName, int t) {
final CheckIn checkIn = checkIns.get(id);
checkIns.remove(id);
final String route = checkIn.stationName + "->" + stationName;
checkOuts.putIfAbsent(route, new CheckOut(0, 0));
++checkOuts.get(route).numTrips;
checkOuts.get(route).totalTime += t - checkIn.time;
}
public double getAverageTime(String startStation, String endStation) {
final CheckOut checkOut = checkOuts.get(startStation + "->" + endStation);
return checkOut.totalTime / (double) checkOut.numTrips;
}
private Map<Integer, CheckIn> checkIns = new HashMap<>();
private Map<String, CheckOut> checkOuts = new HashMap<>(); // {route: (numTrips, totalTime)}
}