forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1396-design-underground-system.js
54 lines (51 loc) · 1.65 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
// https://leetcode.com/problems/design-underground-system/
class UndergroundSystem {
constructor() {
this.stationSystem = {};
this.averageTime = {};
}
/**
* Time O(1) | Space O(1)
* Records the check-in time and station for a user.
* @param {number} id - User ID
* @param {string} stationName - Check-in station name
* @param {number} t - Check-in time
* @return {void}
*/
checkIn(id, stationName, t) {
this.stationSystem[id] = [stationName, '', t, ''];
}
/**
* Time O(1) | Space O(1)
* Records the check-out time and station for a user, and calculates the average time.
* @param {number} id - User ID
* @param {string} stationName - Check-out station name
* @param {number} t - Check-out time
* @return {void}
*/
checkOut(id, stationName, t) {
const user = this.stationSystem[id];
user[1] = stationName;
user[3] = t;
const stationHash = `${user[0]}-${user[1]}`;
if (this.averageTime[stationHash]) {
this.averageTime[stationHash][0] += 1;
this.averageTime[stationHash][1] += user[3] - user[2];
} else {
this.averageTime[stationHash] = [];
this.averageTime[stationHash][0] = 1;
this.averageTime[stationHash][1] = user[3] - user[2];
}
}
/**
* Time O(1) | Space O(1)
* Returns the average time taken to travel between two stations.
* @param {string} startStation - Start station name
* @param {string} endStation - End station name
* @return {number} - Average time in hours
*/
getAverageTime(startStation, endStation) {
const [rounds, totalHours] = this.averageTime[`${startStation}-${endStation}`];
return totalHours / rounds;
}
}