-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1396. Design Underground System.java
82 lines (59 loc) · 2.08 KB
/
1396. Design Underground System.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class UndergroundSystem {
HashMap<Integer, checkinPair> checkinMap; // id, {station name, time}
HashMap<String, checkoutPair> checkoutMap; // route, {time, count}
public UndergroundSystem() {
checkinMap = new HashMap<>();
checkoutMap = new HashMap<>();
}
public void checkIn(int id, String stationName, int t) {
checkinMap.put(id, new checkinPair(stationName, t));
}
public void checkOut(int id, String stationName, int t) {
checkinPair pair = checkinMap.get(id);
String station = pair.stName;
String key = station + "-" + stationName;
int totalTime = t - pair.time;
if(!checkoutMap.containsKey(key))
checkoutMap.put(key, new checkoutPair(totalTime, 1));
else
{
checkoutPair p = checkoutMap.get(key);
int count = p.count;
int time = p.time;
checkoutMap.put(key, new checkoutPair(time + totalTime, count+1));
}
}
public double getAverageTime(String startStation, String endStation) {
String key = startStation + "-" + endStation;
checkoutPair pair = checkoutMap.get(key);
double avg = (double)pair.time/(double)pair.count;
return avg;
}
class checkinPair{
String stName;
int time;
checkinPair(){}
checkinPair(String stName, int time)
{
this.stName = stName;
this.time = time;
}
}
class checkoutPair{
int time;
int count;
checkoutPair(){}
checkoutPair(int time, int count)
{
this.time = time;
this.count = count;
}
}
}
/**
* Your UndergroundSystem object will be instantiated and called as such:
* UndergroundSystem obj = new UndergroundSystem();
* obj.checkIn(id,stationName,t);
* obj.checkOut(id,stationName,t);
* double param_3 = obj.getAverageTime(startStation,endStation);
*/