Skip to content

Commit dc6f58e

Browse files
committed
feat: add solutions to lc problem: No.1396
No.1396.Design Underground System
1 parent 5d60bc5 commit dc6f58e

File tree

6 files changed

+355
-106
lines changed

6 files changed

+355
-106
lines changed

solution/1300-1399/1396.Design Underground System/README.md

+129-34
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,20 @@ undergroundSystem.getAverageTime("Leyton", "Paradise"); // 返回 6.66667 ,(5
100100

101101
<!-- 这里可写通用的实现逻辑 -->
102102

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$ 为乘客的数量。
104117

105118
<!-- tabs:start -->
106119

@@ -110,28 +123,22 @@ undergroundSystem.getAverageTime("Leyton", "Paradise"); // 返回 6.66667 ,(5
110123

111124
```python
112125
class UndergroundSystem:
126+
113127
def __init__(self):
114-
self.check_in_station = {}
115-
self.check_in_time = {}
116-
self.total_time = {}
128+
self.ts = {}
129+
self.d = {}
117130

118131
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)
121133

122134
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)
130138

131139
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]
135142

136143

137144
# Your UndergroundSystem object will be instantiated and called as such:
@@ -147,35 +154,31 @@ class UndergroundSystem:
147154

148155
```java
149156
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<>();
153160

154161
public UndergroundSystem() {
155-
checkInStation = new HashMap<>();
156-
checkInTime = new HashMap<>();
157-
totalTime = new HashMap<>();
162+
158163
}
159164

160165
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);
163168
}
164169

165170
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);
173176
}
174177

175178
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];
179182
}
180183
}
181184

@@ -188,6 +191,98 @@ class UndergroundSystem {
188191
*/
189192
```
190193

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+
191286
### **...**
192287

193288
```

solution/1300-1399/1396.Design Underground System/README_EN.md

+118-36
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,22 @@ undergroundSystem.getAverageTime(&quot;Leyton&quot;, &quot;Paradise&quot;); // r
101101

102102
```python
103103
class UndergroundSystem:
104+
104105
def __init__(self):
105-
self.check_in_station = {}
106-
self.check_in_time = {}
107-
self.total_time = {}
106+
self.ts = {}
107+
self.d = {}
108108

109109
def checkIn(self, id: int, stationName: str, t: int) -> None:
110-
self.check_in_station[id] = stationName
111-
self.check_in_time[id] = t
110+
self.ts[id] = (t, stationName)
112111

113112
def checkOut(self, id: int, stationName: str, t: int) -> None:
114-
cost = t - self.check_in_time.pop(id)
115-
start_station = self.check_in_station.pop(id)
116-
stations = start_station + '.' + stationName
117-
times = self.total_time.get(stations, [0, 0])
118-
times[0] += cost
119-
times[1] += 1
120-
self.total_time[stations] = times
113+
t0, station = self.ts[id]
114+
x = self.d.get((station, stationName), (0, 0))
115+
self.d[(station, stationName)] = (x[0] + t - t0, x[1] + 1)
121116

122117
def getAverageTime(self, startStation: str, endStation: str) -> float:
123-
stations = startStation + '.' + endStation
124-
times = self.total_time[stations]
125-
return times[0] / times[1]
118+
x = self.d[(startStation, endStation)]
119+
return x[0] / x[1]
126120

127121

128122
# Your UndergroundSystem object will be instantiated and called as such:
@@ -136,35 +130,31 @@ class UndergroundSystem:
136130

137131
```java
138132
class UndergroundSystem {
139-
private Map<Integer, String> checkInStation;
140-
private Map<Integer, Integer> checkInTime;
141-
private Map<String, int[]> totalTime;
133+
private Map<Integer, Integer> ts = new HashMap<>();
134+
private Map<Integer, String> names = new HashMap<>();
135+
private Map<String, int[]> d = new HashMap<>();
142136

143137
public UndergroundSystem() {
144-
checkInStation = new HashMap<>();
145-
checkInTime = new HashMap<>();
146-
totalTime = new HashMap<>();
147-
}
148138

139+
}
140+
149141
public void checkIn(int id, String stationName, int t) {
150-
checkInStation.put(id, stationName);
151-
checkInTime.put(id, t);
142+
ts.put(id, t);
143+
names.put(id, stationName);
152144
}
153-
145+
154146
public void checkOut(int id, String stationName, int t) {
155-
int cost = t - checkInTime.remove(id);
156-
String startStation = checkInStation.remove(id);
157-
String stations = startStation + "." + stationName;
158-
int[] times = totalTime.getOrDefault(stations, new int[2]);
159-
times[0] += cost;
160-
++times[1];
161-
totalTime.put(stations, times);
147+
String key = names.get(id) + "-" + stationName;
148+
int[] v = d.getOrDefault(key, new int[2]);
149+
v[0] += t - ts.get(id);
150+
v[1]++;
151+
d.put(key, v);
162152
}
163-
153+
164154
public double getAverageTime(String startStation, String endStation) {
165-
String stations = startStation + "." + endStation;
166-
int[] times = totalTime.get(stations);
167-
return times[0] * 1.0 / times[1];
155+
String key = startStation + "-" + endStation;
156+
int[] v = d.get(key);
157+
return (double) v[0] / v[1];
168158
}
169159
}
170160

@@ -177,6 +167,98 @@ class UndergroundSystem {
177167
*/
178168
```
179169

170+
### **C++**
171+
172+
```cpp
173+
class UndergroundSystem {
174+
public:
175+
UndergroundSystem() {
176+
}
177+
178+
void checkIn(int id, string stationName, int t) {
179+
ts[id] = {stationName, t};
180+
}
181+
182+
void checkOut(int id, string stationName, int t) {
183+
auto [station, t0] = ts[id];
184+
auto key = station + "-" + stationName;
185+
auto [tot, cnt] = d[key];
186+
d[key] = {tot + t - t0, cnt + 1};
187+
}
188+
189+
double getAverageTime(string startStation, string endStation) {
190+
auto [tot, cnt] = d[startStation + "-" + endStation];
191+
return (double) tot / cnt;
192+
}
193+
194+
private:
195+
unordered_map<int, pair<string, int>> ts;
196+
unordered_map<string, pair<int, int>> d;
197+
};
198+
199+
/**
200+
* Your UndergroundSystem object will be instantiated and called as such:
201+
* UndergroundSystem* obj = new UndergroundSystem();
202+
* obj->checkIn(id,stationName,t);
203+
* obj->checkOut(id,stationName,t);
204+
* double param_3 = obj->getAverageTime(startStation,endStation);
205+
*/
206+
```
207+
208+
### **Go**
209+
210+
```go
211+
type UndergroundSystem struct {
212+
ts map[int]pair
213+
d map[station][2]int
214+
}
215+
216+
func Constructor() UndergroundSystem {
217+
return UndergroundSystem{
218+
ts: make(map[int]pair),
219+
d: make(map[station][2]int),
220+
}
221+
}
222+
223+
func (this *UndergroundSystem) CheckIn(id int, stationName string, t int) {
224+
this.ts[id] = pair{t, stationName}
225+
}
226+
227+
func (this *UndergroundSystem) CheckOut(id int, stationName string, t int) {
228+
p := this.ts[id]
229+
s := station{p.a, stationName}
230+
if _, ok := this.d[s]; !ok {
231+
this.d[s] = [2]int{t - p.t, 1}
232+
} else {
233+
this.d[s] = [2]int{this.d[s][0] + t - p.t, this.d[s][1] + 1}
234+
}
235+
236+
}
237+
238+
func (this *UndergroundSystem) GetAverageTime(startStation string, endStation string) float64 {
239+
s := station{startStation, endStation}
240+
return float64(this.d[s][0]) / float64(this.d[s][1])
241+
}
242+
243+
type station struct {
244+
a string
245+
b string
246+
}
247+
248+
type pair struct {
249+
t int
250+
a string
251+
}
252+
253+
/**
254+
* Your UndergroundSystem object will be instantiated and called as such:
255+
* obj := Constructor();
256+
* obj.CheckIn(id,stationName,t);
257+
* obj.CheckOut(id,stationName,t);
258+
* param_3 := obj.GetAverageTime(startStation,endStation);
259+
*/
260+
```
261+
180262
### **...**
181263

182264
```

0 commit comments

Comments
 (0)