-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path1396-Design-Underground-System.js
More file actions
40 lines (36 loc) · 1 KB
/
1396-Design-Underground-System.js
File metadata and controls
40 lines (36 loc) · 1 KB
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
class UndergroundSystem {
constructor() {
this.checkIns = {};
this.trips = {};
}
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
checkIn(id, stationName, t) {
this.checkIns[id] = { t, stationName };
}
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
checkOut(id, stationName, t) {
const route = `${this.checkIns[id].stationName}, ${stationName}`;
if (!(route in this.trips)) this.trips[route] = [];
this.trips[route].push(t - this.checkIns[id].t);
}
/**
* @param {string} startStation
* @param {string} endStation
* @return {number}
*/
getAverageTime(startStation, endStation) {
const route = `${startStation}, ${endStation}`;
const amount = this.trips[route].length;
return this.trips[route].reduce((acc, curr) => acc + curr, 0) / amount;
}
}