-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.go
49 lines (41 loc) · 1.08 KB
/
Solution.go
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
type UndergroundSystem struct {
ts map[int]pair
d map[station][2]int
}
func Constructor() UndergroundSystem {
return UndergroundSystem{
ts: make(map[int]pair),
d: make(map[station][2]int),
}
}
func (this *UndergroundSystem) CheckIn(id int, stationName string, t int) {
this.ts[id] = pair{t, stationName}
}
func (this *UndergroundSystem) CheckOut(id int, stationName string, t int) {
p := this.ts[id]
s := station{p.a, stationName}
if _, ok := this.d[s]; !ok {
this.d[s] = [2]int{t - p.t, 1}
} else {
this.d[s] = [2]int{this.d[s][0] + t - p.t, this.d[s][1] + 1}
}
}
func (this *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
s := station{startStation, endStation}
return float64(this.d[s][0]) / float64(this.d[s][1])
}
type station struct {
a string
b string
}
type pair struct {
t int
a string
}
/**
* Your UndergroundSystem object will be instantiated and called as such:
* obj := Constructor();
* obj.CheckIn(id,stationName,t);
* obj.CheckOut(id,stationName,t);
* param_3 := obj.GetAverageTime(startStation,endStation);
*/