Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package leetcode;
  2.  
  3. import java.util.HashMap;
  4.  
  5. class UndergroundSystem {
  6.   HashMap<Integer, Checkin> checkins = new HashMap<>();
  7.   HashMap<String, HashMap<String, Stats>> statsLookup = new HashMap<>();
  8.  
  9.   public void checkIn(int id, String stationName, int t) {
  10.     checkins.put(id, new Checkin(stationName, t));
  11.   }
  12.  
  13.   public void checkOut(int id, String stationName, int t) {
  14.     Checkin checkin = checkins.get(id);
  15.     statsLookup.put(checkin.stationIn, statsLookup.getOrDefault(checkin.stationIn, new HashMap<>()));
  16.     HashMap<String, Stats> inner = statsLookup.get(checkin.stationIn);
  17.     inner.put(stationName, inner.getOrDefault(stationName, new Stats()));
  18.     inner.get(stationName).samples++;
  19.     inner.get(stationName).totalTime += t - checkin.timeIn;    
  20.   }
  21.  
  22.   public double getAverageTime(String startStation, String endStation) {
  23.     return statsLookup.get(startStation).get(endStation).getAverage();
  24.   }
  25.  
  26.   static class Stats {
  27.     int totalTime = 0;
  28.     int samples = 0;
  29.  
  30.     public double getAverage() {
  31.       return totalTime / (samples + 0.0);
  32.     }
  33.   }
  34.  
  35.   static class Checkin {
  36.     String stationIn;
  37.     int timeIn;
  38.  
  39.     public Checkin(String s, int t) {
  40.       this.stationIn = s;
  41.       this.timeIn = t;
  42.     }
  43.   }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement