|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../count-number-of-teams "Count Number of Teams") |
| 9 | + |
| 10 | +[Next >](../find-all-good-strings "Find All Good Strings") |
| 11 | + |
| 12 | +## [1396. Design Underground System (Medium)](https://leetcode.com/problems/design-underground-system "设计地铁系统") |
| 13 | + |
| 14 | +<p>Implement the class <code>UndergroundSystem</code> that supports three methods:</p> |
| 15 | + |
| 16 | +<p>1.<code> checkIn(int id, string stationName, int t)</code></p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li>A customer with id card equal to <code>id</code>, gets in the station <code>stationName</code> at time <code>t</code>.</li> |
| 20 | + <li>A customer can only be checked into one place at a time.</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p>2.<code> checkOut(int id, string stationName, int t)</code></p> |
| 24 | + |
| 25 | +<ul> |
| 26 | + <li>A customer with id card equal to <code>id</code>, gets out from the station <code>stationName</code> at time <code>t</code>.</li> |
| 27 | +</ul> |
| 28 | + |
| 29 | +<p>3. <code>getAverageTime(string startStation, string endStation)</code> </p> |
| 30 | + |
| 31 | +<ul> |
| 32 | + <li>Returns the average time to travel between the <code>startStation</code> and the <code>endStation</code>.</li> |
| 33 | + <li>The average time is computed from all the previous traveling from <code>startStation</code> to <code>endStation</code> that happened <strong>directly</strong>.</li> |
| 34 | + <li>Call to <code>getAverageTime</code> is always valid.</li> |
| 35 | +</ul> |
| 36 | + |
| 37 | +<p>You can assume all calls to <code>checkIn</code> and <code>checkOut</code> methods are consistent. That is, if a customer gets in at time <strong>t<sub>1</sub></strong> at some station, then it gets out at time <strong>t<sub>2</sub></strong> with <strong>t<sub>2</sub> > t<sub>1</sub></strong>. All events happen in chronological order.</p> |
| 38 | + |
| 39 | +<p> </p> |
| 40 | +<p><strong>Example 1:</strong></p> |
| 41 | + |
| 42 | +<pre> |
| 43 | +<strong>Input</strong> |
| 44 | +["UndergroundSystem","checkIn","checkIn","checkIn","checkOut","checkOut","checkOut","getAverageTime","getAverageTime","checkIn","getAverageTime","checkOut","getAverageTime"] |
| 45 | +[[],[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"]] |
| 46 | + |
| 47 | +<strong>Output</strong> |
| 48 | +[null,null,null,null,null,null,null,14.0,11.0,null,11.0,null,12.0] |
| 49 | + |
| 50 | +<strong>Explanation</strong> |
| 51 | +UndergroundSystem undergroundSystem = new UndergroundSystem(); |
| 52 | +undergroundSystem.checkIn(45, "Leyton", 3); |
| 53 | +undergroundSystem.checkIn(32, "Paradise", 8); |
| 54 | +undergroundSystem.checkIn(27, "Leyton", 10); |
| 55 | +undergroundSystem.checkOut(45, "Waterloo", 15); |
| 56 | +undergroundSystem.checkOut(27, "Waterloo", 20); |
| 57 | +undergroundSystem.checkOut(32, "Cambridge", 22); |
| 58 | +undergroundSystem.getAverageTime("Paradise", "Cambridge"); // return 14.0. There was only one travel from "Paradise" (at time 8) to "Cambridge" (at time 22) |
| 59 | +undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.0. There were two travels from "Leyton" to "Waterloo", a customer with id=45 from time=3 to time=15 and a customer with id=27 from time=10 to time=20. So the average time is ( (15-3) + (20-10) ) / 2 = 11.0 |
| 60 | +undergroundSystem.checkIn(10, "Leyton", 24); |
| 61 | +undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 11.0 |
| 62 | +undergroundSystem.checkOut(10, "Waterloo", 38); |
| 63 | +undergroundSystem.getAverageTime("Leyton", "Waterloo"); // return 12.0</pre> |
| 64 | + |
| 65 | +<p> </p> |
| 66 | +<p><strong>Constraints:</strong></p> |
| 67 | + |
| 68 | +<ul> |
| 69 | + <li>There will be at most <code><font face="monospace">20000</font></code> operations.</li> |
| 70 | + <li><code>1 <= id, t <= 10^6</code></li> |
| 71 | + <li>All strings consist of uppercase, lowercase English letters and digits.</li> |
| 72 | + <li><code>1 <= stationName.length <= 10</code></li> |
| 73 | + <li>Answers within <code>10^-5</code> of the actual value will be accepted as correct.</li> |
| 74 | +</ul> |
| 75 | + |
| 76 | +### Related Topics |
| 77 | + [[Design](../../tag/design/README.md)] |
| 78 | + |
| 79 | +### Hints |
| 80 | +<details> |
| 81 | +<summary>Hint 1</summary> |
| 82 | +Use two hash tables. The first to save the check-in time for a customer and the second to update the total time between two stations. |
| 83 | +</details> |
0 commit comments