|
| 1 | +/** |
| 2 | + * 1396. Design Underground System |
| 3 | + * https://leetcode.com/problems/design-underground-system/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * An underground railway system is keeping track of customer travel times between different |
| 7 | + * stations. They are using this data to calculate the average time it takes to travel from |
| 8 | + * one station to another. |
| 9 | + * |
| 10 | + * Implement the UndergroundSystem class: |
| 11 | + * - void checkIn(int id, string stationName, int t) |
| 12 | + * - A customer with a card ID equal to id, checks in at the station stationName at time t. |
| 13 | + * - A customer can only be checked into one place at a time. |
| 14 | + * - void checkOut(int id, string stationName, int t) |
| 15 | + * - A customer with a card ID equal to id, checks out from the station stationName at time t. |
| 16 | + * - double getAverageTime(string startStation, string endStation) |
| 17 | + * - Returns the average time it takes to travel from startStation to endStation. |
| 18 | + * - The average time is computed from all the previous traveling times from startStation to |
| 19 | + * endStation that happened directly, meaning a check in at startStation followed by a check |
| 20 | + * out from endStation. |
| 21 | + * - The time it takes to travel from startStation to endStation may be different from the |
| 22 | + * time it takes to travel from endStation to startStation. |
| 23 | + * |
| 24 | + * There will be at least one customer that has traveled from startStation to endStation before |
| 25 | + * getAverageTime is called. |
| 26 | + * You may assume all calls to the checkIn and checkOut methods are consistent. If a customer |
| 27 | + * checks in at time t1 then checks out at time t2, then t1 < t2. All events happen in |
| 28 | + * chronological order. |
| 29 | + */ |
| 30 | + |
| 31 | + |
| 32 | +var UndergroundSystem = function() { |
| 33 | + this.travels = new Map(); |
| 34 | + this.averages = new Map(); |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * @param {number} id |
| 39 | + * @param {string} stationName |
| 40 | + * @param {number} t |
| 41 | + * @return {void} |
| 42 | + */ |
| 43 | +UndergroundSystem.prototype.checkIn = function(id, stationName, t) { |
| 44 | + this.travels.set(id, { startStation: stationName, startTime: t }); |
| 45 | +}; |
| 46 | + |
| 47 | +/** |
| 48 | + * @param {number} id |
| 49 | + * @param {string} stationName |
| 50 | + * @param {number} t |
| 51 | + * @return {void} |
| 52 | + */ |
| 53 | +UndergroundSystem.prototype.checkOut = function(id, stationName, t) { |
| 54 | + const { startStation, startTime } = this.travels.get(id); |
| 55 | + const route = `${startStation}-${stationName}`; |
| 56 | + const duration = t - startTime; |
| 57 | + |
| 58 | + if (!this.averages.has(route)) { |
| 59 | + this.averages.set(route, { totalTime: 0, count: 0 }); |
| 60 | + } |
| 61 | + |
| 62 | + const record = this.averages.get(route); |
| 63 | + record.totalTime += duration; |
| 64 | + record.count += 1; |
| 65 | + |
| 66 | + this.travels.delete(id); |
| 67 | +}; |
| 68 | + |
| 69 | +/** |
| 70 | + * @param {string} startStation |
| 71 | + * @param {string} endStation |
| 72 | + * @return {number} |
| 73 | + */ |
| 74 | +UndergroundSystem.prototype.getAverageTime = function(startStation, endStation) { |
| 75 | + const route = `${startStation}-${endStation}`; |
| 76 | + const { totalTime, count } = this.averages.get(route); |
| 77 | + return totalTime / count; |
| 78 | +}; |
0 commit comments