Design Underground System LeetCode solution in JavaScript

1
var UndergroundSystem = function () {
2
this.checkInMap = {};
3
this.tripMap = {};
4
};
5

6
/**
7
* @param {number} id
8
* @param {string} stationName
9
* @param {number} t
10
* @return {void}
11
*/
12
UndergroundSystem.prototype.checkIn = function (id, stationName, t) {
13
this.checkInMap[id] = [stationName, t];
14
};
15

16
/**
17
* @param {number} id
18
* @param {string} stationName
19
* @param {number} t
20
* @return {void}
21
*/
22
UndergroundSystem.prototype.checkOut = function (id, stationName, t) {
23
const [checkInStationName, checkInTime] = this.checkInMap[id];
24
const tripKey = `${checkInStationName}-${stationName}`;
25

26
if (!this.tripMap[tripKey]) this.tripMap[tripKey] = [0, 0];
27

28
this.tripMap[tripKey][0] += t - checkInTime;
29
this.tripMap[tripKey][1]++;
30
};
31

32
/**
33
* @param {string} startStation
34
* @param {string} endStation
35
* @return {number}
36
*/
37
UndergroundSystem.prototype.getAverageTime = function (
38
startStation,
39
endStation
40
) {
41
const tripkey = `${startStation}-${endStation}`;
42
const [totalTripTime, totalTrips] = this.tripMap[tripkey];
43
return totalTripTime / totalTrips;
44
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0