package leetcode;

/**
 * https://leetcode.com/problems/design-underground-system/
 */
public class Problem1396 {
    static class UndergroundSystem {
        public UndergroundSystem() {
        }

        public void checkIn(int id, String stationName, int t) {
            // TODO
        }

        public void checkOut(int id, String stationName, int t) {
            // TODO
        }

        public double getAverageTime(String startStation, String endStation) {
            // TODO
            return 0;
        }
    }
}

@@ -12,16 +12,65 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -193,16 +193,557 @@
 ystem %7B%0A
+        private static class StationTime %7B%0A            private final String station;%0A            private final int time;%0A%0A            public StationTime(String station, int time) %7B%0A                this.station = station;%0A                this.time = time;%0A            %7D%0A        %7D%0A%0A        private static class SumCount %7B%0A            private int sum;%0A            private int count;%0A        %7D%0A%0A        private final Map%3CInteger, StationTime%3E checkIns = new HashMap%3C%3E();%0A        private final Map%3CString, SumCount%3E checkOuts = new HashMap%3C%3E();%0A%0A
         
@@ -851,39 +851,82 @@
 ) %7B%0A            
-// TODO
+checkIns.put(id, new StationTime(stationName, t));
 %0A        %7D%0A%0A    
@@ -1003,146 +1003,725 @@
     
-// TODO%0A        %7D%0A%0A        public double getAverageTime(String startStation, String endStation) %7B%0A            // TODO%0A            return 0
+StationTime from = checkIns.get(id);%0A            String key = key(from.station, stationName);%0A            if (!checkOuts.containsKey(key)) %7B%0A                checkOuts.put(key, new SumCount());%0A            %7D%0A            SumCount sumCount = checkOuts.get(key);%0A            sumCount.sum += t - from.time;%0A            sumCount.count++;%0A            checkIns.remove(id);%0A        %7D%0A%0A        public double getAverageTime(String startStation, String endStation) %7B%0A            SumCount sumCount = checkOuts.get(key(startStation, endStation));%0A            return sumCount.sum / (double) sumCount.count;%0A        %7D%0A%0A        private static String key(String station1, String station2) %7B%0A            return station1 + %22%7C%22 + station2
 ;%0A  

Solve problem 1396