-
Notifications
You must be signed in to change notification settings - Fork 672
/
Copy pathc.go
48 lines (42 loc) · 1.09 KB
/
c.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
package main
type UndergroundSystem struct {
tin map[int]map[string]int
records map[string]map[string][2]int
}
func Constructor() (u UndergroundSystem) {
u.tin = map[int]map[string]int{}
u.records = map[string]map[string][2]int{}
return
}
func (u *UndergroundSystem) CheckIn(id int, stationName string, t int) {
in, ok := u.tin[id]
if !ok {
in = map[string]int{}
}
in[stationName] = t
u.tin[id] = in
}
func (u *UndergroundSystem) CheckOut(id int, stationName string, t int) {
for st, tin := range u.tin[id] {
out, ok := u.records[st]
if !ok {
out = map[string][2]int{}
}
r := out[stationName]
r[0] += t - tin
r[1]++
out[stationName] = r
u.records[st] = out
}
}
func (u *UndergroundSystem) GetAverageTime(startStation string, endStation string) (ans float64) {
r := u.records[startStation][endStation]
return float64(r[0]) / float64(r[1])
}
/**
* 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);
*/