- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1396.java
64 lines (53 loc) · 2.31 KB
/
_1396.java
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
53
54
55
56
57
58
59
60
61
62
63
64
packagecom.fishercoder.solutions.secondthousand;
importjava.util.HashMap;
importjava.util.LinkedList;
importjava.util.Map;
publicclass_1396 {
publicstaticclassSolution1 {
publicclassUndergroundSystem {
classStationAndTime {
StringstationName;
intt;
publicStationAndTime(StringstationName, intt) {
this.stationName = stationName;
this.t = t;
}
publicStringgetStation() {
returnthis.stationName;
}
publicintgetTime() {
returnthis.t;
}
}
Map<String, double[]> averageTimeMap;
Map<Integer, LinkedList<StationAndTime>> travelerMap = newHashMap<>();
publicUndergroundSystem() {
averageTimeMap = newHashMap<>();
travelerMap = newHashMap<>();
}
publicvoidcheckIn(intid, StringstationName, intt) {
if (!travelerMap.containsKey(id)) {
travelerMap.put(id, newLinkedList<>());
}
travelerMap.get(id).add(newStationAndTime(stationName, t));
}
publicvoidcheckOut(intid, StringstationName, intt) {
LinkedList<StationAndTime> list = travelerMap.get(id);
StationAndTimestationAndTime = list.getLast();
StringstartToEndStation = stationAndTime.getStation() + "->" + stationName;
intduration = t - stationAndTime.getTime();
if (!averageTimeMap.containsKey(startToEndStation)) {
averageTimeMap.put(startToEndStation, newdouble[] {duration, 1});
} else {
double[] pair = averageTimeMap.get(startToEndStation);
doublenewAverage =
(double) (pair[0] * pair[1] + duration) / (double) (pair[1] + 1);
averageTimeMap.put(startToEndStation, newdouble[] {newAverage, pair[1] + 1});
}
}
publicdoublegetAverageTime(StringstartStation, StringendStation) {
returnaverageTimeMap.get(startStation + "->" + endStation)[0];
}
}
}
}