1+ class UndergroundSystem {
2+ // id, src, time
3+ Map <Integer , Pair <String , Integer >> entries ;
4+ // src, dest, int[total time, number of travels]
5+ Map <String , Map <String , int []>> times ;
6+
7+ public UndergroundSystem () {
8+ this .entries = new HashMap <>();
9+ this .times = new HashMap <>();
10+ }
11+
12+ public void checkIn (int id , String stationName , int t ) {
13+ entries .put (id , new Pair <>(stationName , t ));
14+ }
15+
16+ public void checkOut (int id , String dest , int t ) {
17+ Pair <String , Integer > entry = entries .get (id );
18+ String src = entry .getKey ();
19+ int duration = t - entry .getValue ();
20+
21+ Map <String , int []> city = times .getOrDefault (src , new HashMap <>());
22+ int [] total = city .getOrDefault (dest , new int [2 ]);
23+ total [0 ] += duration ;
24+ total [1 ]++;
25+ city .put (dest , total );
26+ times .put (src , city );
27+ }
28+
29+ public double getAverageTime (String startStation , String endStation ) {
30+ int [] total = times .get (startStation ).get (endStation );
31+ return 1.0 * total [0 ] / total [1 ];
32+ }
33+ }
34+
35+ /**
36+ * Your UndergroundSystem object will be instantiated and called as such:
37+ * UndergroundSystem obj = new UndergroundSystem();
38+ * obj.checkIn(id,stationName,t);
39+ * obj.checkOut(id,stationName,t);
40+ * double param_3 = obj.getAverageTime(startStation,endStation);
41+ */
0 commit comments