@@ -100,7 +100,20 @@ undergroundSystem.getAverageTime("Leyton", "Paradise"); // 返回 6.66667 ,(5
100
100
101
101
<!-- 这里可写通用的实现逻辑 -->
102
102
103
- “哈希表”实现。
103
+ ** 方法一:哈希表**
104
+
105
+ 我们用两个哈希表来存储数据,其中:
106
+
107
+ - ` ts ` :存储乘客的 id 和乘客的进站时间和进站站点。其中键为乘客的 id,值为元组 ` (t, stationName) ` 。
108
+ - ` d ` :存储乘客的进站站点和出站站点,以及乘客的行程时间和行程次数。其中键为元组 ` (startStation, endStation) ` ,值为元组 ` (totalTime, count) ` 。
109
+
110
+ 当乘客进站时,我们将乘客的 id 和进站时间和进站站点存入 ` ts ` 中,即 ` ts[id] = (t, stationName) ` 。
111
+
112
+ 当乘客出站时,我们从 ` ts ` 中取出乘客的进站时间和进站站点 ` (t0, station) ` ,然后计算乘客的行程时间 $t - t_0$,并将乘客的行程时间和行程次数存入 ` d ` 中。
113
+
114
+ 当我们要求某个乘客的平均行程时间时,我们从 ` d ` 中取出乘客的行程时间和行程次数 ` (totalTime, count) ` ,然后计算平均行程时间 $totalTime / count$ 即可。
115
+
116
+ 时间复杂度 $O(1)$,空间复杂度 $O(n)$。其中 $n$ 为乘客的数量。
104
117
105
118
<!-- tabs:start -->
106
119
@@ -110,28 +123,22 @@ undergroundSystem.getAverageTime("Leyton", "Paradise"); // 返回 6.66667 ,(5
110
123
111
124
``` python
112
125
class UndergroundSystem :
126
+
113
127
def __init__ (self ):
114
- self .check_in_station = {}
115
- self .check_in_time = {}
116
- self .total_time = {}
128
+ self .ts = {}
129
+ self .d = {}
117
130
118
131
def checkIn (self , id : int , stationName : str , t : int ) -> None :
119
- self .check_in_station[id ] = stationName
120
- self .check_in_time[id ] = t
132
+ self .ts[id ] = (t, stationName)
121
133
122
134
def checkOut (self , id : int , stationName : str , t : int ) -> None :
123
- cost = t - self .check_in_time.pop(id )
124
- start_station = self .check_in_station.pop(id )
125
- stations = start_station + ' .' + stationName
126
- times = self .total_time.get(stations, [0 , 0 ])
127
- times[0 ] += cost
128
- times[1 ] += 1
129
- self .total_time[stations] = times
135
+ t0, station = self .ts[id ]
136
+ x = self .d.get((station, stationName), (0 , 0 ))
137
+ self .d[(station, stationName)] = (x[0 ] + t - t0, x[1 ] + 1 )
130
138
131
139
def getAverageTime (self , startStation : str , endStation : str ) -> float :
132
- stations = startStation + ' .' + endStation
133
- times = self .total_time[stations]
134
- return times[0 ] / times[1 ]
140
+ x = self .d[(startStation, endStation)]
141
+ return x[0 ] / x[1 ]
135
142
136
143
137
144
# Your UndergroundSystem object will be instantiated and called as such:
@@ -147,35 +154,31 @@ class UndergroundSystem:
147
154
148
155
``` java
149
156
class UndergroundSystem {
150
- private Map<Integer , String > checkInStation ;
151
- private Map<Integer , Integer > checkInTime ;
152
- private Map<String , int[]> totalTime ;
157
+ private Map<Integer , Integer > ts = new HashMap<> () ;
158
+ private Map<Integer , String > names = new HashMap<> () ;
159
+ private Map<String , int[]> d = new HashMap<> () ;
153
160
154
161
public UndergroundSystem () {
155
- checkInStation = new HashMap<> ();
156
- checkInTime = new HashMap<> ();
157
- totalTime = new HashMap<> ();
162
+
158
163
}
159
164
160
165
public void checkIn (int id , String stationName , int t ) {
161
- checkInStation . put(id, stationName );
162
- checkInTime . put(id, t );
166
+ ts . put(id, t );
167
+ names . put(id, stationName );
163
168
}
164
169
165
170
public void checkOut (int id , String stationName , int t ) {
166
- int cost = t - checkInTime. remove(id);
167
- String startStation = checkInStation. remove(id);
168
- String stations = startStation + " ." + stationName;
169
- int [] times = totalTime. getOrDefault(stations, new int [2 ]);
170
- times[0 ] += cost;
171
- ++ times[1 ];
172
- totalTime. put(stations, times);
171
+ String key = names. get(id) + " -" + stationName;
172
+ int [] v = d. getOrDefault(key, new int [2 ]);
173
+ v[0 ] += t - ts. get(id);
174
+ v[1 ]++ ;
175
+ d. put(key, v);
173
176
}
174
177
175
178
public double getAverageTime (String startStation , String endStation ) {
176
- String stations = startStation + " . " + endStation;
177
- int [] times = totalTime . get(stations );
178
- return times [0 ] * 1.0 / times [1 ];
179
+ String key = startStation + " - " + endStation;
180
+ int [] v = d . get(key );
181
+ return ( double ) v [0 ] / v [1 ];
179
182
}
180
183
}
181
184
@@ -188,6 +191,98 @@ class UndergroundSystem {
188
191
*/
189
192
```
190
193
194
+ ### ** C++**
195
+
196
+ ``` cpp
197
+ class UndergroundSystem {
198
+ public:
199
+ UndergroundSystem() {
200
+ }
201
+
202
+ void checkIn(int id, string stationName, int t) {
203
+ ts[id] = {stationName, t};
204
+ }
205
+
206
+ void checkOut (int id, string stationName, int t) {
207
+ auto [ station, t0] = ts[ id] ;
208
+ auto key = station + "-" + stationName;
209
+ auto [ tot, cnt] = d[ key] ;
210
+ d[ key] = {tot + t - t0, cnt + 1};
211
+ }
212
+
213
+ double getAverageTime(string startStation, string endStation) {
214
+ auto [tot, cnt] = d[startStation + "-" + endStation];
215
+ return (double) tot / cnt;
216
+ }
217
+
218
+ private:
219
+ unordered_map<int, pair<string, int>> ts;
220
+ unordered_map<string, pair<int, int>> d;
221
+ };
222
+
223
+ /**
224
+ * Your UndergroundSystem object will be instantiated and called as such:
225
+ * UndergroundSystem* obj = new UndergroundSystem();
226
+ * obj->checkIn(id,stationName,t);
227
+ * obj->checkOut(id,stationName,t);
228
+ * double param_3 = obj->getAverageTime(startStation,endStation);
229
+ * /
230
+ ```
231
+
232
+ ### **Go**
233
+
234
+ ```go
235
+ type UndergroundSystem struct {
236
+ ts map[int]pair
237
+ d map[station][2]int
238
+ }
239
+
240
+ func Constructor() UndergroundSystem {
241
+ return UndergroundSystem{
242
+ ts: make(map[int]pair),
243
+ d: make(map[station][2]int),
244
+ }
245
+ }
246
+
247
+ func (this *UndergroundSystem) CheckIn(id int, stationName string, t int) {
248
+ this.ts[id] = pair{t, stationName}
249
+ }
250
+
251
+ func (this *UndergroundSystem) CheckOut(id int, stationName string, t int) {
252
+ p := this.ts[id]
253
+ s := station{p.a, stationName}
254
+ if _, ok := this.d[s]; !ok {
255
+ this.d[s] = [2]int{t - p.t, 1}
256
+ } else {
257
+ this.d[s] = [2]int{this.d[s][0] + t - p.t, this.d[s][1] + 1}
258
+ }
259
+
260
+ }
261
+
262
+ func (this *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
263
+ s := station{startStation, endStation}
264
+ return float64(this.d[s][0]) / float64(this.d[s][1])
265
+ }
266
+
267
+ type station struct {
268
+ a string
269
+ b string
270
+ }
271
+
272
+ type pair struct {
273
+ t int
274
+ a string
275
+ }
276
+
277
+ /**
278
+ * Your UndergroundSystem object will be instantiated and called as such:
279
+ * obj := Constructor();
280
+ * obj.CheckIn(id,stationName,t);
281
+ * obj.CheckOut(id,stationName,t);
282
+ * param_3 := obj.GetAverageTime(startStation,endStation);
283
+ */
284
+ ```
285
+
191
286
### ** ...**
192
287
193
288
```
0 commit comments