1396. Design Underground System Medium
1/**
2 * [1396] Design Underground System
3 *
4 * 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.
5 * Implement the UndergroundSystem class:
6 *
7 * void checkIn(int id, string stationName, int t)
8 *
9 * A customer with a card ID equal to id, checks in at the station stationName at time t.
10 * A customer can only be checked into one place at a time.
11 *
12 *
13 * void checkOut(int id, string stationName, int t)
14 *
15 * A customer with a card ID equal to id, checks out from the station stationName at time t.
16 *
17 *
18 * double getAverageTime(string startStation, string endStation)
19 *
20 * Returns the average time it takes to travel from startStation to endStation.
21 * 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.
22 * The time it takes to travel from startStation to endStation may be different from the time it takes to travel from endStation to startStation.
23 * There will be at least one customer that has traveled from startStation to endStation before getAverageTime is called.
24 *
25 *
26 *
27 * 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.
28 *
29 * Example 1:
30 *
31 * Input
32 * ["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"]
33 * [[],[45,"Leyton",3],[32,"Paradise",8],[27,"Leyton",10],[45,"Waterloo",15],[27,"Waterloo",20],[32,"Cambridge",22],["Paradise","Cambridge"],["Leyton","Waterloo"],[10,"Leyton",24],["Leyton","Waterloo"],[10,"Waterloo",38],["Leyton","Waterloo"]]
34 * Output
35 * [null,null,null,null,null,null,null,14.00000,11.00000,null,11.00000,null,12.00000]
36 * Explanation
37 * UndergroundSystem undergroundSystem = new UndergroundSystem();
38 * undergroundSystem.checkIn(45, "Leyton", 3);
39 * undergroundSystem.checkIn(32, "Paradise", 8);
40 * undergroundSystem.checkIn(27, "Leyton", 10);
41 * undergroundSystem.checkOut(45, "Waterloo", 15); // Customer 45 "Leyton" -> "Waterloo" in 15-3 = 12
42 * undergroundSystem.checkOut(27, "Waterloo", 20); // Customer 27 "Leyton" -> "Waterloo" in 20-10 = 10
43 * undergroundSystem.checkOut(32, "Cambridge", 22); // Customer 32 "Paradise" -> "Cambridge" in 22-8 = 14
44 * undergroundSystem.getAverageTime("Paradise", "Cambridge"); // return 14.00000. One trip "Paradise" -> "Cambridge", (14) / 1 = 14
45 * undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.00000. Two trips "Leyton" -> "Waterloo", (10 + 12) / 2 = 11
46 * undergroundSystem.checkIn(10, "Leyton", 24);
47 * undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.00000
48 * undergroundSystem.checkOut(10, "Waterloo", 38); // Customer 10 "Leyton" -> "Waterloo" in 38-24 = 14
49 * undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 12.00000. Three trips "Leyton" -> "Waterloo", (10 + 12 + 14) / 3 = 12
50 *
51 * Example 2:
52 *
53 * Input
54 * ["UndergroundSystem","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime","checkIn","checkOut","getAverageTime"]
55 * [[],[10,"Leyton",3],[10,"Paradise",8],["Leyton","Paradise"],[5,"Leyton",10],[5,"Paradise",16],["Leyton","Paradise"],[2,"Leyton",21],[2,"Paradise",30],["Leyton","Paradise"]]
56 * Output
57 * [null,null,null,5.00000,null,null,5.50000,null,null,6.66667]
58 * Explanation
59 * UndergroundSystem undergroundSystem = new UndergroundSystem();
60 * undergroundSystem.checkIn(10, "Leyton", 3);
61 * undergroundSystem.checkOut(10, "Paradise", 8); // Customer 10 "Leyton" -> "Paradise" in 8-3 = 5
62 * undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.00000, (5) / 1 = 5
63 * undergroundSystem.checkIn(5, "Leyton", 10);
64 * undergroundSystem.checkOut(5, "Paradise", 16); // Customer 5 "Leyton" -> "Paradise" in 16-10 = 6
65 * undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 5.50000, (5 + 6) / 2 = 5.5
66 * undergroundSystem.checkIn(2, "Leyton", 21);
67 * undergroundSystem.checkOut(2, "Paradise", 30); // Customer 2 "Leyton" -> "Paradise" in 30-21 = 9
68 * undergroundSystem.getAverageTime("Leyton", "Paradise"); // return 6.66667, (5 + 6 + 9) / 3 = 6.66667
69 *
70 *
71 * Constraints:
72 *
73 * 1 <= id, t <= 10^6
74 * 1 <= stationName.length, startStation.length, endStation.length <= 10
75 * All strings consist of uppercase and lowercase English letters and digits.
76 * There will be at most 2 * 10^4 calls in total to checkIn, checkOut, and getAverageTime.
77 * Answers within 10^-5 of the actual value will be accepted.
78 *
79 */
80pub struct Solution {}
81
82// problem: https://leetcode.com/problems/design-underground-system/
83// discuss: https://leetcode.com/problems/design-underground-system/discuss/?currentPage=1&orderBy=most_votes&query=
84
85// submission codes start here
86
87struct UndergroundSystem {
88 false
89 }
90
91
92/**
93 * `&self` means the method takes an immutable reference.
94 * If you need a mutable reference, change it to `&mut self` instead.
95 */
96impl UndergroundSystem {
97
98 fn new() -> Self {
99
100 }
101
102 fn check_in(&self, id: i32, station_name: String, t: i32) {
103
104 }
105
106 fn check_out(&self, id: i32, station_name: String, t: i32) {
107
108 }
109
110 fn get_average_time(&self, start_station: String, end_station: String) -> f64 {
111
112 }
113}
114
115/**
116 * Your UndergroundSystem object will be instantiated and called as such:
117 * let obj = UndergroundSystem::new();
118 * obj.check_in(id, stationName, t);
119 * obj.check_out(id, stationName, t);
120 * let ret_3: f64 = obj.get_average_time(startStation, endStation);
121 */
122
123// submission codes end
124
125#[cfg(test)]
126mod tests {
127 use super::*;
128
129 #[test]
130 fn test_1396() {
131 }
132}
133
Back
© 2025 bowen.ge All Rights Reserved.