-
Notifications
You must be signed in to change notification settings - Fork 0
/
1396.design-underground-system.go
71 lines (55 loc) · 1.85 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* @lc app=leetcode id=1396 lang=golang
*
* [1396] Design Underground System
*/
// @lc code=start
type Record struct {
startStation string
time int
}
type UndergroundSystem struct {
passenger map[int]Record
// key: pair of (start station, end station)
// value: [record of travel time, total travel counter]
trafficRecord map[[2]string][2]int
}
func Constructor() UndergroundSystem {
return UndergroundSystem{
make(map[int]Record),
make(map[[2]string][2]int),
}
}
func (this *UndergroundSystem) CheckIn(id int, stationName string, t int) {
this.passenger[id] = Record{
stationName,
t,
}
return
}
func (this *UndergroundSystem) CheckOut(id int, stationName string, t int) {
// update current traversal into corresponding traffic record
latestRecord := this.passenger[id]
startStation, startTime := latestRecord.startStation, latestRecord.time
endStation, endTime := stationName, t
trafficKey := [2]string{startStation, endStation}
// get prev total travel time and prev total traverl counter
prevTraffic := this.trafficRecord[trafficKey]
prevTraverlTimeSum, prevTraverlCounter := prevTraffic[0], prevTraffic[1]
this.trafficRecord[trafficKey] = [2]int{prevTraverlTimeSum + (endTime - startTime), prevTraverlCounter + 1}
return
}
func (this *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
// get latest traffic info from station pair
trafficInfo := this.trafficRecord[[2]string{startStation, endStation}]
totalTraverlTime, totalTraverlCounter := trafficInfo[0], trafficInfo[1]
return float64(totalTraverlTime) / float64(totalTraverlCounter)
}
/**
* 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);
*/
// @lc code=end