题目

Implement the class UndergroundSystem that supports three methods:

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

  • A customer with id card equal to id, gets in the station stationName at time t.
  • A customer can only be checked into one place at a time.

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

  • A customer with id card equal to id, gets out from the station stationName at time t.

3.getAverageTime(string startStation, string endStation)

  • Returns the average time to travel between the startStation and the endStation.
  • The average time is computed from all the previous traveling from startStation to endStation that happened directly.
  • Call to getAverageTime is always valid.

You can assume all calls to checkIn and checkOut methods are consistent. That is, if a customer gets in at time t1 at some station, then it gets out at time t2 with t2 > t1. All events happen in chronological order.

Example 1:

  1. Input
  2. ["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
  3. [[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]]
  4. Output
  5. [null,null,null,null,null,null,null,14.0,11.0,null,11.0,null,12.0]
  6. Explanation
  7. UndergroundSystem undergroundSystem = new UndergroundSystem();
  8. undergroundSystem.checkIn(45, "Leyton", 3);
  9. undergroundSystem.checkIn(32, "Paradise", 8);
  10. undergroundSystem.checkIn(27, "Leyton", 10);
  11. undergroundSystem.checkOut(45, "Waterloo", 15);
  12. undergroundSystem.checkOut(27, "Waterloo", 20);
  13. undergroundSystem.checkOut(32, "Cambridge", 22);
  14. undergroundSystem.getAverageTime("Paradise", "Cambridge"); // return 14.0. There was only one travel from "Paradise" (at time 8) to "Cambridge" (at time 22)
  15. undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.0. There were two travels from "Leyton" to "Waterloo", a customer with id=45 from time=3 to time=15 and a customer with id=27 from time=10 to time=20. So the average time is ( (15-3) + (20-10) ) / 2 = 11.0
  16. undergroundSystem.checkIn(10, "Leyton", 24);
  17. undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.0
  18. undergroundSystem.checkOut(10, "Waterloo", 38);
  19. undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 12.0

Constraints:

  • There will be at most 20000 operations.
  • 1 <= id, t <= 10^6
  • All strings consist of uppercase, lowercase English letters and digits.
  • 1 <= stationName.length <= 10
  • Answers within 10^-5 of the actual value will be accepted as correct.

题意

实现一个地铁系统类的三个方法。

思路

使用两个HashMap处理:

  1. 第一个map记录编号id的乘客的入站信息;
  2. 第二个map记录线路’A-B’对应的行驶总用时和行驶次数。

代码实现

Java

  1. class UndergroundSystem {
  2. private Map<Integer, Object[]> start;
  3. private Map<String, int[]> route;
  4. public UndergroundSystem() {
  5. start = new HashMap<>();
  6. route = new HashMap<>();
  7. }
  8. public void checkIn(int id, String stationName, int t) {
  9. start.putIfAbsent(id, new Object[2]);
  10. Object[] list = start.get(id);
  11. list[0] = stationName;
  12. list[1] = t;
  13. }
  14. public void checkOut(int id, String stationName, int t) {
  15. Object[] info = start.get(id);
  16. String routeName = (String) info[0] + '-' + stationName;
  17. int prev = (int) info[1];
  18. route.putIfAbsent(routeName, new int[2]);
  19. int[] list = route.get(routeName);
  20. list[0] += t - prev;
  21. list[1]++;
  22. }
  23. public double getAverageTime(String startStation, String endStation) {
  24. String routeName = startStation + '-' + endStation;
  25. int[] list = route.get(routeName);
  26. return 1.0 * list[0] / list[1];
  27. }
  28. }

JavaScript

  1. var UndergroundSystem = function () {
  2. this.travel = new Map()
  3. this.record = new Map()
  4. }
  5. /**
  6. * @param {number} id
  7. * @param {string} stationName
  8. * @param {number} t
  9. * @return {void}
  10. */
  11. UndergroundSystem.prototype.checkIn = function (id, stationName, t) {
  12. this.travel.set(id, [stationName, t])
  13. }
  14. /**
  15. * @param {number} id
  16. * @param {string} stationName
  17. * @param {number} t
  18. * @return {void}
  19. */
  20. UndergroundSystem.prototype.checkOut = function (id, stationName, t) {
  21. const from = this.travel.get(id)[0]
  22. const to = stationName
  23. const interval = t - this.travel.get(id)[1]
  24. if (!this.record.has(from)) {
  25. this.record.set(from, new Map())
  26. }
  27. if (!this.record.get(from).has(to)) {
  28. this.record.get(from).set(to, [])
  29. }
  30. this.record.get(from).get(to).push(interval)
  31. this.travel.delete(id)
  32. }
  33. /**
  34. * @param {string} startStation
  35. * @param {string} endStation
  36. * @return {number}
  37. */
  38. UndergroundSystem.prototype.getAverageTime = function (startStation, endStation) {
  39. const arr = this.record.get(startStation).get(endStation)
  40. return arr.reduce((acc, cur) => acc + cur) / arr.length
  41. }