Skip to content

Commit 7c55f54

Browse files
committed
Create 1396.设计地铁系统.js
1 parent 7553127 commit 7c55f54

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

1396.设计地铁系统.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
var UndergroundSystem = function() {
3+
this.in = new Map();
4+
this.check = new Map();
5+
};
6+
7+
/**
8+
* @param {number} id
9+
* @param {string} stationName
10+
* @param {number} t
11+
* @return {void}
12+
*/
13+
UndergroundSystem.prototype.checkIn = function(id, stationName, t) {
14+
this.in.set(id, [stationName, t]);
15+
};
16+
17+
/**
18+
* @param {number} id
19+
* @param {string} stationName
20+
* @param {number} t
21+
* @return {void}
22+
*/
23+
UndergroundSystem.prototype.checkOut = function(id, stationName, t) {
24+
const [a, b] = this.in.get(id);
25+
const key = a + ':' + stationName;
26+
const check = this.check.get(key);
27+
if (check) {
28+
this.check.set(key, [t - b + check[0], 1 + check[1]]);
29+
} else {
30+
this.check.set(key, [t - b, 1]);
31+
}
32+
};
33+
34+
/**
35+
* @param {string} startStation
36+
* @param {string} endStation
37+
* @return {number}
38+
*/
39+
UndergroundSystem.prototype.getAverageTime = function(startStation, endStation) {
40+
const key = startStation + ':' + endStation;
41+
const check = this.check.get(key);
42+
if (!check) {
43+
return 0;
44+
} else {
45+
return check[0] / check[1];
46+
}
47+
};
48+
49+
/**
50+
* Your UndergroundSystem object will be instantiated and called as such:
51+
* var obj = new UndergroundSystem()
52+
* obj.checkIn(id,stationName,t)
53+
* obj.checkOut(id,stationName,t)
54+
* var param_3 = obj.getAverageTime(startStation,endStation)
55+
*/

0 commit comments

Comments
 (0)