-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1396.设计地铁系统.js
55 lines (50 loc) · 1.31 KB
/
1396.设计地铁系统.js
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
var UndergroundSystem = function() {
this.in = new Map();
this.check = new Map();
};
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkIn = function(id, stationName, t) {
this.in.set(id, [stationName, t]);
};
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkOut = function(id, stationName, t) {
const [a, b] = this.in.get(id);
const key = a + ':' + stationName;
const check = this.check.get(key);
if (check) {
this.check.set(key, [t - b + check[0], 1 + check[1]]);
} else {
this.check.set(key, [t - b, 1]);
}
};
/**
* @param {string} startStation
* @param {string} endStation
* @return {number}
*/
UndergroundSystem.prototype.getAverageTime = function(startStation, endStation) {
const key = startStation + ':' + endStation;
const check = this.check.get(key);
if (!check) {
return 0;
} else {
return check[0] / check[1];
}
};
/**
* Your UndergroundSystem object will be instantiated and called as such:
* var obj = new UndergroundSystem()
* obj.checkIn(id,stationName,t)
* obj.checkOut(id,stationName,t)
* var param_3 = obj.getAverageTime(startStation,endStation)
*/