|
| 1 | +// 1396. Design Underground System |
| 2 | +// 🟠 Medium |
| 3 | +// |
| 4 | +// https://leetcode.com/problems/design-underground-system/ |
| 5 | +// |
| 6 | +// Tags: Hash Table - Design - String |
| 7 | + |
| 8 | +use std::collections::HashMap; |
| 9 | + |
| 10 | +struct UndergroundSystem { |
| 11 | + travelers: HashMap<i32, (String, i32)>, |
| 12 | + times: HashMap<(String, String), (f64, usize)>, |
| 13 | +} |
| 14 | + |
| 15 | +/// Use two hashmaps, one to store travelers in transit information, it will |
| 16 | +/// save an entry when a traveler starts a trip with the traveler's id as the |
| 17 | +/// key and the station and time as the value. When a traveler checks out, it |
| 18 | +/// will use all the data to compute the travel time and insert it into the |
| 19 | +/// times hashmap under the (start_st, end_st) key, if the key already exists |
| 20 | +/// in the hashmap, it will update the values. When we need to get the average, |
| 21 | +/// we access the times hashmap and use the total traveled time and number of |
| 22 | +/// trips to get and return it. |
| 23 | +/// |
| 24 | +/// Time complexity: O(1) - Each call to any of the methods is processed in |
| 25 | +/// constant time. |
| 26 | +/// Space complexity: O(n) - We could have as many entries in the travelers |
| 27 | +/// hashmap as calls to the check_in method, but most likely that number will |
| 28 | +/// remain low, as entries are removed by calls to the checkout method. The |
| 29 | +/// number of entries in the times hashmap could be equal to the number of |
| 30 | +/// total trips, if every combination of in_station and out_station was unique. |
| 31 | +/// |
| 32 | +/// Runtime 34 ms Beats 92.86% |
| 33 | +/// Memory 17.1 MB Beats 100% |
| 34 | +impl UndergroundSystem { |
| 35 | + fn new() -> Self { |
| 36 | + UndergroundSystem { |
| 37 | + travelers: HashMap::new(), |
| 38 | + times: HashMap::new(), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + fn check_in(&mut self, id: i32, station_name: String, t: i32) { |
| 43 | + self.travelers.insert(id, (station_name, t)); |
| 44 | + } |
| 45 | + |
| 46 | + fn check_out(&mut self, id: i32, station_name: String, t: i32) { |
| 47 | + match self.travelers.remove(&id) { |
| 48 | + Some((in_st, in_tm)) => { |
| 49 | + let travel_time = (t - in_tm) as f64; |
| 50 | + let src_station = in_st.to_owned(); |
| 51 | + self.times |
| 52 | + .entry((src_station, station_name)) |
| 53 | + .and_modify(|(total_travel_time, count)| { |
| 54 | + *total_travel_time += travel_time; |
| 55 | + *count += 1; |
| 56 | + }) |
| 57 | + .or_insert((travel_time, 1)); |
| 58 | + } |
| 59 | + None => unreachable!(), |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + fn get_average_time(&self, start_station: String, end_station: String) -> f64 { |
| 64 | + match self.times.get(&(start_station, end_station)) { |
| 65 | + Some(e) => e.0 / e.1 as f64, |
| 66 | + None => unreachable!(), |
| 67 | + } |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Your UndergroundSystem object will be instantiated and called as such: |
| 73 | + * let obj = UndergroundSystem::new(); |
| 74 | + * obj.check_in(id, stationName, t); |
| 75 | + * obj.check_out(id, stationName, t); |
| 76 | + * let ret_3: f64 = obj.get_average_time(startStation, endStation); |
| 77 | + */ |
| 78 | + |
| 79 | +/** |
| 80 | + * Your MyHashSet object will be instantiated and called as such: |
| 81 | + * let obj = MyHashSet::new(); |
| 82 | + * obj.add(key); |
| 83 | + * obj.remove(key); |
| 84 | + * let ret_3: bool = obj.contains(key); |
| 85 | + */ |
| 86 | +fn main() { |
| 87 | + let mut us = UndergroundSystem::new(); |
| 88 | + us.check_in(10, String::from("Leyton"), 3); |
| 89 | + us.check_out(10, String::from("Paradise"), 8); |
| 90 | + assert_eq!( |
| 91 | + us.get_average_time(String::from("Leyton"), String::from("Paradise")), |
| 92 | + 5.0 |
| 93 | + ); |
| 94 | + us.check_in(5, String::from("Leyton"), 10); |
| 95 | + us.check_out(5, String::from("Paradise"), 16); |
| 96 | + assert_eq!( |
| 97 | + us.get_average_time(String::from("Leyton"), String::from("Paradise")), |
| 98 | + 5.5 |
| 99 | + ); |
| 100 | + us.check_in(2, String::from("Leyton"), 21); |
| 101 | + us.check_out(2, String::from("Paradise"), 30); |
| 102 | + assert!( |
| 103 | + us.get_average_time(String::from("Leyton"), String::from("Paradise")) - (20.0 / 3.0) |
| 104 | + < 0.00001 |
| 105 | + ); |
| 106 | + println!("\x1b[92m» All tests passed!\x1b[0m") |
| 107 | +} |
0 commit comments