-
Notifications
You must be signed in to change notification settings - Fork 481
/
1396.js
39 lines (35 loc) · 1.05 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
var UndergroundSystem = function() {
this.check_in = new Map();
this.check_out = new Map();
};
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkIn = function(id, stationName, t) {
this.check_in.set(id, [stationName, t]);
};
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkOut = function(id, stationName, t) {
let [startName, timeIn] = this.check_in.get(id);
let name = startName + ">" + stationName;
if (!this.check_out.has(name)) this.check_out.set(name, [0, 0]);
let [avg, cnt] = this.check_out.get(name);
this.check_out.set(name, [avg + t - timeIn, cnt + 1]);
};
/**
* @param {string} startStation
* @param {string} endStation
* @return {number}
*/
UndergroundSystem.prototype.getAverageTime = function(startStation, endStation) {
let name = startStation + ">" + endStation;
return this.check_out.get(name)[0] / this.check_out.get(name)[1];
};