-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path1396. Design Underground System.go
55 lines (47 loc) · 1.34 KB
/
1396. Design Underground System.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
50
51
52
53
54
55
package leetcode
type checkin struct {
station string
time int
}
type stationTime struct {
sum, count float64
}
type UndergroundSystem struct {
checkins map[int]*checkin
stationTimes map[string]map[string]*stationTime
}
func Constructor() UndergroundSystem {
return UndergroundSystem{
make(map[int]*checkin),
make(map[string]map[string]*stationTime),
}
}
func (s *UndergroundSystem) CheckIn(id int, stationName string, t int) {
s.checkins[id] = &checkin{stationName, t}
}
func (s *UndergroundSystem) CheckOut(id int, stationName string, t int) {
checkin := s.checkins[id]
destination := s.stationTimes[checkin.station]
if destination == nil {
s.stationTimes[checkin.station] = make(map[string]*stationTime)
}
st := s.stationTimes[checkin.station][stationName]
if st == nil {
st = new(stationTime)
s.stationTimes[checkin.station][stationName] = st
}
st.sum += float64(t - checkin.time)
st.count++
delete(s.checkins, id)
}
func (s *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
st := s.stationTimes[startStation][endStation]
return st.sum / st.count
}
/**
* 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);
*/