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
|
package main
import "fmt"
type Trip struct {
station string
t int
}
type Info struct {
count int
sum int
}
type UndergroundSystem struct {
checkInTable map[int]Trip
timeTable map[string]Info
}
func Constructor() UndergroundSystem {
var us UndergroundSystem
us.checkInTable = make(map[int]Trip)
us.timeTable = make(map[string]Info)
return us
}
func (this *UndergroundSystem) CheckIn(id int, stationName string, t int) {
this.checkInTable[id] = Trip{station: stationName, t: t}
}
func (this *UndergroundSystem) CheckOut(id int, stationName string, t int) {
begin := this.checkInTable[id].station
key := begin + ":" + stationName
time := t - this.checkInTable[id].t
entry, _ := this.timeTable[key]
entry.count++
entry.sum += time
this.timeTable[key] = entry
}
func (this *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
key := startStation + ":" + endStation
return float64(this.timeTable[key].sum) / float64(this.timeTable[key].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);
*/
func main() {
undergroundSystem := Constructor()
undergroundSystem.CheckIn(10, "Leyton", 3)
undergroundSystem.CheckOut(10, "Paradise", 8) // Customer 10 "Leyton" -> "Paradise" in 8-3 = 5
fmt.Println(undergroundSystem.GetAverageTime("Leyton", "Paradise")) // return 5.00000, (5) / 1 = 5
undergroundSystem.CheckIn(5, "Leyton", 10)
undergroundSystem.CheckOut(5, "Paradise", 16) // Customer 5 "Leyton" -> "Paradise" in 16-10 = 6
fmt.Println(undergroundSystem.GetAverageTime("Leyton", "Paradise")) // return 5.50000, (5 + 6) / 2 = 5.5
undergroundSystem.CheckIn(2, "Leyton", 21)
undergroundSystem.CheckOut(2, "Paradise", 30) // Customer 2 "Leyton" -> "Paradise" in 30-21 = 9
fmt.Println(undergroundSystem.GetAverageTime("Leyton", "Paradise")) // return 6.66667, (5 + 6 + 9) / 3 = 6.66667
}
|