diff --git a/May/Day 31/1396_Design_Underground_System.java b/May/Day 31/1396_Design_Underground_System.java new file mode 100644 index 0000000..3e6390f --- /dev/null +++ b/May/Day 31/1396_Design_Underground_System.java @@ -0,0 +1,37 @@ +class UndergroundSystem { + private Map> checkIns = new HashMap<>(); + private Map, int[]> times = new HashMap<>(); + + public UndergroundSystem() { + + } + + public void checkIn(int id, String stationName, int t) { + checkIns.put(id, new Pair(stationName, t)); + } + + public void checkOut(int id, String stationName, int t) { + var startStation = checkIns.get(id).getKey(); + var startTime = checkIns.get(id).getValue(); + checkIns.remove(id); + + var pair = new Pair(startStation, stationName); + var totalTime = times.containsKey(pair) ? times.get(pair)[0] : 0; + var dataPoints = times.containsKey(pair) ? times.get(pair)[1] : 0; + + times.put(pair, new int[]{totalTime + t - startTime, dataPoints + 1}); + } + + public double getAverageTime(String startStation, String endStation) { + var pair = new Pair(startStation, endStation); + return (double) times.get(pair)[0] / times.get(pair)[1]; + } +} + +/** + * 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); + */ \ No newline at end of file