-
Notifications
You must be signed in to change notification settings - Fork 0
/
1396.design-underground-system.java
58 lines (51 loc) · 1.71 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
class UndergroundSystem {
class Pair{
int a;
String place;
Double c;
int b;
Pair(int a, String place){
this.a = a;
this.place = place;
}
Pair(Double c, int b){
this.c =c;
this.b = b;
}
@Override
public String toString(){
return this.a+" b "+this.b+" c "+this.c+" place "+this.place;
}
}
HashMap<Integer, Pair> in;
// HashMap<Integer, Pair> out;
HashMap<String , Pair> avgRes;
public UndergroundSystem() {
in = new HashMap<>();
// out = new HashMap<>();
avgRes = new HashMap<>();
}
public void checkIn(int id, String stationName, int t) {
in.put(id, new Pair(t, stationName));
}
public void checkOut(int id, String stationName, int t) {
String key = in.get(id).place+"$"+stationName;
// System.out.println(key+" bef "+avgRes.get(key)+" "+(t-in.get(id).a));
if(!avgRes.containsKey(key))
avgRes.put(key, new Pair((double)(t- in.get(id).a), 1));
else{
Double val =(double)((avgRes.get(key).c*(avgRes.get(key).b))+(t-in.get(id).a))/(avgRes.get(key).b+1);
avgRes.put(key, new Pair(val, avgRes.get(key).b+1));
}
}
public double getAverageTime(String startStation, String endStation) {
return avgRes.get(startStation+"$"+endStation).c;
}
}
/**
* 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);
*/