Skip to content

Commit e37f67a

Browse files
authored
Create 1396-design-underground-system.js
1 parent bbc49a3 commit e37f67a

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

1396-design-underground-system.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
const UndergroundSystem = function() {
3+
this.h = new Map()
4+
this.routeMap = 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.h.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 [sn, st] = this.h.get(id)
25+
this.h.delete(id)
26+
const route = `${sn},${stationName}`
27+
const duration = t - st
28+
const [totalTime, totalValue] = this.routeMap.get(route) || ([0, 0])
29+
this.routeMap.set(route, [totalTime + duration, totalValue + 1])
30+
};
31+
32+
/**
33+
* @param {string} startStation
34+
* @param {string} endStation
35+
* @return {number}
36+
*/
37+
UndergroundSystem.prototype.getAverageTime = function(startStation, endStation) {
38+
const k = `${startStation},${endStation}`
39+
const [time, number] = this.routeMap.get(k)
40+
return time / number
41+
};
42+
43+
/**
44+
* Your UndergroundSystem object will be instantiated and called as such:
45+
* var obj = new UndergroundSystem()
46+
* obj.checkIn(id,stationName,t)
47+
* obj.checkOut(id,stationName,t)
48+
* var param_3 = obj.getAverageTime(startStation,endStation)
49+
*/

0 commit comments

Comments
 (0)