aboutsummaryrefslogtreecommitdiffstats
path: root/1396
diff options
context:
space:
mode:
authorterminaldweller <thabogre@gmail.com>2022-04-24 18:02:12 +0000
committerterminaldweller <thabogre@gmail.com>2022-04-24 18:02:12 +0000
commit9808196ebbe8d815c3fa2458462f58a9349c87c2 (patch)
tree7f7e9ada1b678aa7120a6de021189ca531966272 /1396
parentupdate (diff)
downloadleetcode-9808196ebbe8d815c3fa2458462f58a9349c87c2.tar.gz
leetcode-9808196ebbe8d815c3fa2458462f58a9349c87c2.zip
update
Diffstat (limited to '1396')
-rw-r--r--1396/go.mod3
-rw-r--r--1396/main.go66
2 files changed, 69 insertions, 0 deletions
diff --git a/1396/go.mod b/1396/go.mod
new file mode 100644
index 0000000..f35f79c
--- /dev/null
+++ b/1396/go.mod
@@ -0,0 +1,3 @@
+module 1396
+
+go 1.17
diff --git a/1396/main.go b/1396/main.go
new file mode 100644
index 0000000..2af27fd
--- /dev/null
+++ b/1396/main.go
@@ -0,0 +1,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
+}