1396. Diseño del sistema subterráneo

Implemente la clase  UndergroundSystem que admite tres métodos:

1) checkIn(int id, string stationName, int t)

  • Un cliente con una tarjeta de identificación igual a  id, llega a la estación  stationName a la hora  t.
  • Un cliente solo puede registrarse en un lugar a la vez.

2) checkOut(int id, string stationName, int t)

  • Un cliente con una tarjeta de identificación igual a  id, sale de la estación  stationName a la hora  t.

3) getAverageTime(string startStation, string endStation) 

  • Devuelve el tiempo promedio de viaje entre el  startStation y el  endStation.
  • El tiempo promedio se calcula a partir de todos los viajes anteriores de  startStation que  endStation eso ocurriera directamente.
  • Llamar a  getAverageTime siempre es válido.

Puede asumir que todas las llamadas  checkIn y  checkOut métodos son consistentes. Es decir, si un cliente ingresa en el momento t1 en alguna estación, entonces sale en el momento t2 con t2> t1. Todos los eventos ocurren en orden cronológico.

 

Ejemplo 1:

Entrada


00000] Explicación 
UndergroundSystem undergroundSystem = new UndergroundSystem (); 
undergroundSystem.checkIn (45, "Leyton", 3); 
undergroundSystem.checkIn (32, "Paraíso", 8); 


undergroundSystem.checkIn (27, "Leyton", 10);
undergroundSystem.checkOut (45, "Waterloo", 15); 
undergroundSystem.getAverageTime ("Leyton", "Waterloo"); // devuelve 11.00000
undergroundSystem.checkOut (27, "Waterloo", 20); 
undergroundSystem.checkOut (32, "Cambridge", 22); 
undergroundSystem.getAverageTime ("Paraíso", "Cambridge"); // devuelve 14.00000. Solo hubo un viaje desde "Paradise" (en el tiempo 8) a "Cambridge" (en el tiempo 22) 
undergroundSystem.getAverageTime ("Leyton", "Waterloo"); // devuelve 11.00000. Hubo dos viajes de "Leyton" a "Waterloo", un cliente con id = 45 de time = 3 a time = 15 y un cliente con id = 27 de time = 10 a time = 20. Entonces el tiempo promedio es ((15-3) + (20-10)) / 2 = 11.00000 
undergroundSystem.checkIn (10, "Leyton", 24); 
undergroundSystem.checkOut (10, "Waterloo", 38); 
undergroundSystem.getAverageTime ("Leyton", "Waterloo"); // devuelve 12.00000

Ejemplo 2

Entrada

["UndergroundSystem", "checkIn", "checkOut", "getAverageTime", "checkIn", "checkOut", "getAverageTime", "checkIn", "checkOut", "getAverageTime"] 
[[], [10, "Leyton ", 3], [10," Paraíso ", 8], [" Leyton "," Paraíso "], [5," Leyton ", 10], [5," Paraíso ", 16], [" Leyton ", "Paraíso"], [2, "Leyton", 21], [2, "Paraíso", 30], ["Leyton", "Paraíso"]] 

Salida 
[nulo, nulo, nulo, 5.00000, nulo, nulo, 5.50000 , nulo, nulo, 6.66667] 

Explicación 
UndergroundSystem undergroundSystem = new UndergroundSystem (); 
undergroundSystem.checkIn (10, "Leyton", 3); 
undergroundSystem.checkOut (10, "
undergroundSystem.getAverageTime ("Leyton", "Paradise"); // devuelve 5.50000 
undergroundSystem.checkIn (2, "Leyton", 21); 
undergroundSystem.checkOut (2, "Paraíso", 30); 
undergroundSystem.getAverageTime ("Leyton", "Paradise"); // devuelve 6.66667

 

Restricciones:

  • Habrá en la mayoría de las  20000 operaciones.
  • 1 <= id, t <= 10^6
  • Todas las cadenas consisten en letras mayúsculas, minúsculas en inglés y dígitos.
  • 1 <= stationName.length <= 10
  • Las respuestas dentro  10^-5 del valor real serán aceptadas como correctas.
clase UndergroundSystem { 
    HashMap <String, Pair <Integer, Integer >> checkoutMap = new HashMap <> (); // Ruta - {TotalTime, Count} 
    HashMap <Integer, Pair <String, Integer >> checkInMap = new HashMap <> ();  // Uid - {StationName, Time} 
    
    public UndergroundSystem () {} 
    
    public  void checkIn ( int id, String stationName, int t) { 
        checkInMap.put (id, new Pair <> (stationName, t)); 
    } 
    
    public  void checkOut ( int id, String stationName, intt) { 
        Pair <String, Integer> checkIn = checkInMap.get (id); 
        Cadena ruta = checkIn.getKey () + "_" + stationName;
        int totalTime = t - checkIn.getValue (); 
        Pair <Integer, Integer> checkout = checkoutMap.getOrDefault (ruta, nuevo Pair <> (0, 0 )); 
        checkoutMap.put (route, new Pair <> (checkout.getKey () + totalTime, checkout.getValue () + 1 )); 
    } 
    
    public  double getAverageTime (String startStation, String endStation) { 
        String route = startStation + "_" + endStation;
        Par <Integer, Integer> checkout = checkoutMap.get (ruta);
        return ( doble ) checkout.getKey () / checkout.getValue (); 
    } 
}

 

Supongo que te gusta

Origin www.cnblogs.com/wentiliangkaihua/p/12717922.html
Recomendado
Clasificación