-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1396.swift
35 lines (26 loc) · 1.04 KB
/
1396.swift
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
class UndergroundSystem {
private var trips = [Int: (from: String, t1: Int)]()
private var sum = [String: [(to: String, t1: Int, t2: Int)]]()
init() {
}
func checkIn(_ id: Int, _ stationName: String, _ t: Int) {
trips[id] = (stationName, t)
}
func checkOut(_ id: Int, _ stationName: String, _ t: Int) {
let (from, t1) = trips[id]!
trips[id] = nil
sum[from, default: [(to: String, t1: Int, t2: Int)]()].append((stationName, t1, t))
}
func getAverageTime(_ startStation: String, _ endStation: String) -> Double {
let data = sum[startStation]!.filter { $0.to == endStation }
let total = data .map { $0.t2 - $0.t1 } .reduce(0, +)
return Double(total) / Double(data.count)
}
}
/**
* Your UndergroundSystem object will be instantiated and called as such:
* let obj = UndergroundSystem()
* obj.checkIn(id, stationName, t)
* obj.checkOut(id, stationName, t)
* let ret_3: Double = obj.getAverageTime(startStation, endStation)
*/