|
1 |
| -var UndergroundSystem = function() { |
| 1 | +// https://leetcode.com/problems/design-underground-system/ |
| 2 | +class UndergroundSystem { |
| 3 | + constructor() { |
2 | 4 | this.stationSystem = {};
|
3 | 5 | this.averageTime = {};
|
4 |
| -}; |
| 6 | + } |
5 | 7 |
|
6 |
| -/** |
7 |
| - * https://leetcode.com/problems/design-underground-system |
8 |
| - * Time O(1) | Space O(1) |
9 |
| - * @param {number} id |
10 |
| - * @param {string} stationName |
11 |
| - * @param {number} t |
12 |
| - * @return {void} |
13 |
| - */ |
14 |
| -UndergroundSystem.prototype.checkIn = function(id, stationName, t) { |
| 8 | + /** |
| 9 | + * Time O(1) | Space O(1) |
| 10 | + * Records the check-in time and station for a user. |
| 11 | + * @param {number} id - User ID |
| 12 | + * @param {string} stationName - Check-in station name |
| 13 | + * @param {number} t - Check-in time |
| 14 | + * @return {void} |
| 15 | + */ |
| 16 | + checkIn(id, stationName, t) { |
15 | 17 | this.stationSystem[id] = [stationName, '', t, ''];
|
16 |
| -}; |
| 18 | + } |
17 | 19 |
|
18 |
| -/** Time O(1) | Space O(1) |
19 |
| - * @param {number} id |
20 |
| - * @param {string} stationName |
21 |
| - * @param {number} t |
22 |
| - * @return {void} |
23 |
| - */ |
24 |
| -UndergroundSystem.prototype.checkOut = function(id, stationName, t) { |
| 20 | + /** |
| 21 | + * Time O(1) | Space O(1) |
| 22 | + * Records the check-out time and station for a user, and calculates the average time. |
| 23 | + * @param {number} id - User ID |
| 24 | + * @param {string} stationName - Check-out station name |
| 25 | + * @param {number} t - Check-out time |
| 26 | + * @return {void} |
| 27 | + */ |
| 28 | + checkOut(id, stationName, t) { |
25 | 29 | const user = this.stationSystem[id];
|
26 |
| - //1 is the position where we store end station |
27 |
| - // 3 is the position where we store end time |
28 | 30 | user[1] = stationName;
|
29 |
| - user[3] = t; |
30 |
| - console.log(user); |
| 31 | + user[3] = t; |
31 | 32 | const stationHash = `${user[0]}-${user[1]}`;
|
32 |
| - // console.log(stationHash, this.stationHash); |
33 |
| - if(this.averageTime[stationHash]) { |
34 |
| - this.averageTime[stationHash][0] += 1; |
35 |
| - this.averageTime[stationHash][1] += user[3] - user[2]; |
| 33 | + if (this.averageTime[stationHash]) { |
| 34 | + this.averageTime[stationHash][0] += 1; |
| 35 | + this.averageTime[stationHash][1] += user[3] - user[2]; |
36 | 36 | } else {
|
37 |
| - this.averageTime[stationHash] = []; |
38 |
| - this.averageTime[stationHash][0] = 1; |
39 |
| - this.averageTime[stationHash][1] = user[3] - user[2]; |
| 37 | + this.averageTime[stationHash] = []; |
| 38 | + this.averageTime[stationHash][0] = 1; |
| 39 | + this.averageTime[stationHash][1] = user[3] - user[2]; |
40 | 40 | }
|
41 |
| -}; |
| 41 | + } |
42 | 42 |
|
43 |
| -/** Time O(1) | Space O(1) |
44 |
| - * @param {string} startStation |
45 |
| - * @param {string} endStation |
46 |
| - * @return {number} |
47 |
| - */ |
48 |
| -UndergroundSystem.prototype.getAverageTime = function(startStation, endStation) { |
49 |
| - // console.log(this.averageTime); |
| 43 | + /** |
| 44 | + * Time O(1) | Space O(1) |
| 45 | + * Returns the average time taken to travel between two stations. |
| 46 | + * @param {string} startStation - Start station name |
| 47 | + * @param {string} endStation - End station name |
| 48 | + * @return {number} - Average time in hours |
| 49 | + */ |
| 50 | + getAverageTime(startStation, endStation) { |
50 | 51 | const [rounds, totalHours] = this.averageTime[`${startStation}-${endStation}`];
|
51 | 52 | return totalHours / rounds;
|
52 |
| -}; |
53 |
| - |
54 |
| -/** |
55 |
| - * Your UndergroundSystem object will be instantiated and called as such: |
56 |
| - * var obj = new UndergroundSystem() |
57 |
| - * obj.checkIn(id,stationName,t) |
58 |
| - * obj.checkOut(id,stationName,t) |
59 |
| - * var param_3 = obj.getAverageTime(startStation,endStation) |
60 |
| - */ |
| 53 | + } |
| 54 | +} |
0 commit comments