-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1396-design-underground-system.js
78 lines (70 loc) · 2.73 KB
/
1396-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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* 1396. Design Underground System
* https://leetcode.com/problems/design-underground-system/
* Difficulty: Medium
*
* An underground railway system is keeping track of customer travel times between different
* stations. They are using this data to calculate the average time it takes to travel from
* one station to another.
*
* Implement the UndergroundSystem class:
* - void checkIn(int id, string stationName, int t)
* - A customer with a card ID equal to id, checks in at the station stationName at time t.
* - A customer can only be checked into one place at a time.
* - void checkOut(int id, string stationName, int t)
* - A customer with a card ID equal to id, checks out from the station stationName at time t.
* - double getAverageTime(string startStation, string endStation)
* - Returns the average time it takes to travel from startStation to endStation.
* - The average time is computed from all the previous traveling times from startStation to
* endStation that happened directly, meaning a check in at startStation followed by a check
* out from endStation.
* - The time it takes to travel from startStation to endStation may be different from the
* time it takes to travel from endStation to startStation.
*
* There will be at least one customer that has traveled from startStation to endStation before
* getAverageTime is called.
* You may assume all calls to the checkIn and checkOut methods are consistent. If a customer
* checks in at time t1 then checks out at time t2, then t1 < t2. All events happen in
* chronological order.
*/
var UndergroundSystem = function() {
this.travels = new Map();
this.averages = new Map();
};
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkIn = function(id, stationName, t) {
this.travels.set(id, { startStation: stationName, startTime: t });
};
/**
* @param {number} id
* @param {string} stationName
* @param {number} t
* @return {void}
*/
UndergroundSystem.prototype.checkOut = function(id, stationName, t) {
const { startStation, startTime } = this.travels.get(id);
const route = `${startStation}-${stationName}`;
const duration = t - startTime;
if (!this.averages.has(route)) {
this.averages.set(route, { totalTime: 0, count: 0 });
}
const record = this.averages.get(route);
record.totalTime += duration;
record.count += 1;
this.travels.delete(id);
};
/**
* @param {string} startStation
* @param {string} endStation
* @return {number}
*/
UndergroundSystem.prototype.getAverageTime = function(startStation, endStation) {
const route = `${startStation}-${endStation}`;
const { totalTime, count } = this.averages.get(route);
return totalTime / count;
};