-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathDesign Underground System.js
41 lines (36 loc) · 1.06 KB
/
Design Underground System.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
var UndergroundSystem = function() {
this.checkInMap = {}
this.tripMap = {}
};
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkIn = function(id, stationName, t) {
this.checkInMap[id] = [stationName, t]
};
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkOut = function(id, stationName, t) {
const [checkInStationName, checkInTime] = this.checkInMap[id]
const tripKey = `${checkInStationName}-${stationName}`
if (!this.tripMap[tripKey]) this.tripMap[tripKey] = [0,0]
this.tripMap[tripKey][0] += t - checkInTime
this.tripMap[tripKey][1]++
};
/**
* @param {string} startStation
* @param {string} endStation
* @return {number}
*/
UndergroundSystem.prototype.getAverageTime = function(startStation, endStation) {
const tripkey = `${startStation}-${endStation}`
const [totalTripTime, totalTrips] = this.tripMap[tripkey]
return totalTripTime / totalTrips
};