Skip to content

Commit 37ec5ac

Browse files
committed
Add solution #1396
1 parent 344d609 commit 37ec5ac

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,278 LeetCode solutions in JavaScript
1+
# 1,279 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1065,6 +1065,7 @@
10651065
1392|[Longest Happy Prefix](./solutions/1392-longest-happy-prefix.js)|Hard|
10661066
1394|[Find Lucky Integer in an Array](./solutions/1394-find-lucky-integer-in-an-array.js)|Easy|
10671067
1395|[Count Number of Teams](./solutions/1395-count-number-of-teams.js)|Medium|
1068+
1396|[Design Underground System](./solutions/1396-design-underground-system.js)|Medium|
10681069
1400|[Construct K Palindrome Strings](./solutions/1400-construct-k-palindrome-strings.js)|Medium|
10691070
1402|[Reducing Dishes](./solutions/1402-reducing-dishes.js)|Hard|
10701071
1408|[String Matching in an Array](./solutions/1408-string-matching-in-an-array.js)|Easy|
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)