[LeetCode] 1396. Design Underground System

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:

Input
["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
[[],[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"]]

Output
[null,null,null,null,null,null,null,14.00000,11.00000,null,11.00000,null,12.00000]

Explanation
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(45, "Leyton", 3);
undergroundSystem.checkIn(32, "Paradise", 8);
undergroundSystem.checkIn(27, "Leyton", 10);
undergroundSystem.checkOut(45, "Waterloo", 15);
undergroundSystem.checkOut(27, "Waterloo", 20);
undergroundSystem.checkOut(32, "Cambridge", 22);
undergroundSystem.getAverageTime("Paradise", "Cambridge");       // return 14.00000. There was only one travel from "Paradise" (at time 8) to "Cambridge" (at time 22)
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.00000. 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.00000
undergroundSystem.checkIn(10, "Leyton", 24);
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 11.00000
undergroundSystem.checkOut(10, "Waterloo", 38);
undergroundSystem.getAverageTime("Leyton", "Waterloo");          // return 12.00000

Example 2:

Input
["UndergroundSystem","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime"]
[[],[10,"Leyton",3],[10,"Paradise",8],["Leyton","Paradise"],[5,"Leyton",10],[5,"Paradise",16],["Leyton","Paradise"],[2,"Leyton",21],[2,"Paradise",30],["Leyton","Paradise"]]

Output
[null,null,null,5.00000,null,null,5.50000,null,null,6.66667]

Explanation
UndergroundSystem undergroundSystem = new UndergroundSystem();
undergroundSystem.checkIn(10, "Leyton", 3);
undergroundSystem.checkOut(10, "Paradise", 8);
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.00000
undergroundSystem.checkIn(5, "Leyton", 10);
undergroundSystem.checkOut(5, "Paradise", 16);
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.50000
undergroundSystem.checkIn(2, "Leyton", 21);
undergroundSystem.checkOut(2, "Paradise", 30);
undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 6.66667

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.

设计地铁系统。

请你实现一个类 UndergroundSystem ,它支持以下 3 种方法:

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

编号为 id 的乘客在 t 时刻进入地铁站 stationName 。
一个乘客在同一时间只能在一个地铁站进入或者离开。
2. checkOut(int id, string stationName, int t)

编号为 id 的乘客在 t 时刻离开地铁站 stationName 。
3. getAverageTime(string startStation, string endStation) 

返回从地铁站 startStation 到地铁站 endStation 的平均花费时间。
平均时间计算的行程包括当前为止所有从 startStation 直接到达 endStation 的行程。
调用 getAverageTime 时,询问的路线至少包含一趟行程。
你可以假设所有对 checkIn 和 checkOut 的调用都是符合逻辑的。也就是说,如果一个顾客在 t1 时刻到达某个地铁站,那么他离开的时间 t2 一定满足 t2 > t1 。所有的事件都按时间顺序给出。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-underground-system
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题我们需要两个hashmap,一个叫做checkinMap,记录每个乘客checkin的细节,一个叫做checkoutMap,记录每个route的细节,route包含了上车地点和下车地点。其中checkinMap是记录乘客的id,<开始车站,时间>;checkoutMap是记录route,<时间,count>。这里我们需要用到Java 8的一个新功能叫做Pair<,他可以存一些键值对,用法跟hashmap几乎一样。

checkIn()函数。当有乘客check in,我们就把他的ID,<上车地点stationName,时间戳t>记录在checkinMap里。

checkOut()函数。当有乘客下车,我们首先去checkinMap拿到他的上车信息,这样才能拼接他的路径。路径由上车地点 + "_" + 下车地点组成。下车地点由checkOut()函数提供了。同时因为乘客下车了,我们需要结算他的乘车时间 = 下车时间 - 上车时间。

得到路径和乘车时间之后,我们需要把这两个信息放入checkoutMap里。因为有可能有多人有相同的路径,存住这个信息,才能算接下来的getAverageTime。

getAverageTime()函数。将给的上车地点和下车地点拼接成route,把这个route当做key去checkoutMap里找是否存在,若存在,则拿出那个对应的pair,pair里存的是所有当前route的花费时间的总和,除以次数,即得到平均时间。

时间 - N/A

空间 - N/A

Java实现

 1 class UndergroundSystem {
 2     // 乘客的id,<开始车站,时间>
 3     HashMap<Integer, Pair<String, Integer>> checkinMap = new HashMap<>();
 4     // route,<总计时间,相同route的出现次数>
 5     HashMap<String, Pair<Integer, Integer>> checkoutMap = new HashMap<>();
 6 
 7     public UndergroundSystem() {
 8 
 9     }
10 
11     public void checkIn(int id, String stationName, int t) {
12         // 乘客的id,<开始车站,时间>
13         checkinMap.put(id, new Pair<>(stationName, t));
14     }
15 
16     public void checkOut(int id, String stationName, int t) {
17         // 拿到乘客的<开始车站,时间>
18         Pair<String, Integer> checkInDetail = checkinMap.get(id);
19         // 拼接route
20         String route = checkInDetail.getKey() + "_" + stationName;
21         // 花费时间
22         int totalTime = t - checkInDetail.getValue();
23         Pair<Integer, Integer> checkout = checkoutMap.getOrDefault(route, new Pair<>(0, 0));
24         checkoutMap.put(route, new Pair<>(checkout.getKey() + totalTime, checkout.getValue() + 1));
25     }
26 
27     public double getAverageTime(String startStation, String endStation) {
28         String route = startStation + "_" + endStation;
29         Pair<Integer, Integer> checkout = checkoutMap.get(route);
30         return (double) checkout.getKey() / checkout.getValue();
31     }
32 }
33 
34 /**
35  * Your UndergroundSystem object will be instantiated and called as such:
36  * UndergroundSystem obj = new UndergroundSystem();
37  * obj.checkIn(id,stationName,t);
38  * obj.checkOut(id,stationName,t);
39  * double param_3 = obj.getAverageTime(startStation,endStation);
40  */

 

LeetCode 题目总结

上一篇:山东大学软件学院暑期项目实训记录(二)


下一篇:Core Text 雕虫小技: 建立指示行