Skip to content

Commit 58b49e3

Browse files
committed
feat: add solutions to lc problem: No.1396. Design Underground System
1 parent 87f4b78 commit 58b49e3

File tree

4 files changed

+215
-6
lines changed

4 files changed

+215
-6
lines changed

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

+73-3
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,97 @@ undergroundSystem.getAverageTime("Leyton", "Waterloo");
6969
<li>与标准答案误差在&nbsp;<code>10^-5</code>&nbsp;以内的结果都视为正确结果。</li>
7070
</ul>
7171

72-
7372
## 解法
7473

7574
<!-- 这里可写通用的实现逻辑 -->
7675

76+
“哈希表”实现。
77+
7778
<!-- tabs:start -->
7879

7980
### **Python3**
8081

8182
<!-- 这里可写当前语言的特殊实现逻辑 -->
8283

8384
```python
84-
85+
class UndergroundSystem:
86+
87+
def __init__(self):
88+
self.check_in_station = {}
89+
self.check_in_time = {}
90+
self.total_time = {}
91+
92+
def checkIn(self, id: int, stationName: str, t: int) -> None:
93+
self.check_in_station[id] = stationName
94+
self.check_in_time[id] = t
95+
96+
def checkOut(self, id: int, stationName: str, t: int) -> None:
97+
cost = t - self.check_in_time.pop(id)
98+
start_station = self.check_in_station.pop(id)
99+
stations = start_station + '.' + stationName
100+
times = self.total_time.get(stations, [0, 0])
101+
times[0] += cost
102+
times[1] += 1
103+
self.total_time[stations] = times
104+
105+
def getAverageTime(self, startStation: str, endStation: str) -> float:
106+
stations = startStation + '.' + endStation
107+
times = self.total_time[stations]
108+
return times[0] / times[1]
109+
110+
111+
# Your UndergroundSystem object will be instantiated and called as such:
112+
# obj = UndergroundSystem()
113+
# obj.checkIn(id,stationName,t)
114+
# obj.checkOut(id,stationName,t)
115+
# param_3 = obj.getAverageTime(startStation,endStation)
85116
```
86117

87118
### **Java**
88119

89120
<!-- 这里可写当前语言的特殊实现逻辑 -->
90121

91122
```java
92-
123+
class UndergroundSystem {
124+
private Map<Integer, String> checkInStation;
125+
private Map<Integer, Integer> checkInTime;
126+
private Map<String, int[]> totalTime;
127+
128+
public UndergroundSystem() {
129+
checkInStation = new HashMap<>();
130+
checkInTime = new HashMap<>();
131+
totalTime = new HashMap<>();
132+
}
133+
134+
public void checkIn(int id, String stationName, int t) {
135+
checkInStation.put(id, stationName);
136+
checkInTime.put(id, t);
137+
}
138+
139+
public void checkOut(int id, String stationName, int t) {
140+
int cost = t - checkInTime.remove(id);
141+
String startStation = checkInStation.remove(id);
142+
String stations = startStation + "." + stationName;
143+
int[] times = totalTime.getOrDefault(stations, new int[2]);
144+
times[0] += cost;
145+
++times[1];
146+
totalTime.put(stations, times);
147+
}
148+
149+
public double getAverageTime(String startStation, String endStation) {
150+
String stations = startStation + "." + endStation;
151+
int[] times = totalTime.get(stations);
152+
return times[0] * 1.0 / times[1];
153+
}
154+
}
155+
156+
/**
157+
* Your UndergroundSystem object will be instantiated and called as such:
158+
* UndergroundSystem obj = new UndergroundSystem();
159+
* obj.checkIn(id,stationName,t);
160+
* obj.checkOut(id,stationName,t);
161+
* double param_3 = obj.getAverageTime(startStation,endStation);
162+
*/
93163
```
94164

95165
### **...**

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

+71-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
<ul>
1212
<li><code>void checkIn(int id, string stationName, int t)</code>
13-
1413
<ul>
1514
<li>A customer with a card ID equal to <code>id</code>, checks in at the station <code>stationName</code> at time <code>t</code>.</li>
1615
<li>A customer can only be checked into one place at a time.</li>
@@ -102,13 +101,82 @@ undergroundSystem.getAverageTime(&quot;Leyton&quot;, &quot;Paradise&quot;); // r
102101
### **Python3**
103102

104103
```python
105-
104+
class UndergroundSystem:
105+
106+
def __init__(self):
107+
self.check_in_station = {}
108+
self.check_in_time = {}
109+
self.total_time = {}
110+
111+
def checkIn(self, id: int, stationName: str, t: int) -> None:
112+
self.check_in_station[id] = stationName
113+
self.check_in_time[id] = t
114+
115+
def checkOut(self, id: int, stationName: str, t: int) -> None:
116+
cost = t - self.check_in_time.pop(id)
117+
start_station = self.check_in_station.pop(id)
118+
stations = start_station + '.' + stationName
119+
times = self.total_time.get(stations, [0, 0])
120+
times[0] += cost
121+
times[1] += 1
122+
self.total_time[stations] = times
123+
124+
def getAverageTime(self, startStation: str, endStation: str) -> float:
125+
stations = startStation + '.' + endStation
126+
times = self.total_time[stations]
127+
return times[0] / times[1]
128+
129+
130+
# Your UndergroundSystem object will be instantiated and called as such:
131+
# obj = UndergroundSystem()
132+
# obj.checkIn(id,stationName,t)
133+
# obj.checkOut(id,stationName,t)
134+
# param_3 = obj.getAverageTime(startStation,endStation)
106135
```
107136

108137
### **Java**
109138

110139
```java
111-
140+
class UndergroundSystem {
141+
private Map<Integer, String> checkInStation;
142+
private Map<Integer, Integer> checkInTime;
143+
private Map<String, int[]> totalTime;
144+
145+
public UndergroundSystem() {
146+
checkInStation = new HashMap<>();
147+
checkInTime = new HashMap<>();
148+
totalTime = new HashMap<>();
149+
}
150+
151+
public void checkIn(int id, String stationName, int t) {
152+
checkInStation.put(id, stationName);
153+
checkInTime.put(id, t);
154+
}
155+
156+
public void checkOut(int id, String stationName, int t) {
157+
int cost = t - checkInTime.remove(id);
158+
String startStation = checkInStation.remove(id);
159+
String stations = startStation + "." + stationName;
160+
int[] times = totalTime.getOrDefault(stations, new int[2]);
161+
times[0] += cost;
162+
++times[1];
163+
totalTime.put(stations, times);
164+
}
165+
166+
public double getAverageTime(String startStation, String endStation) {
167+
String stations = startStation + "." + endStation;
168+
int[] times = totalTime.get(stations);
169+
return times[0] * 1.0 / times[1];
170+
}
171+
}
172+
173+
/**
174+
* Your UndergroundSystem object will be instantiated and called as such:
175+
* UndergroundSystem obj = new UndergroundSystem();
176+
* obj.checkIn(id,stationName,t);
177+
* obj.checkOut(id,stationName,t);
178+
* double param_3 = obj.getAverageTime(startStation,endStation);
179+
*/
112180
```
113181

114182
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class UndergroundSystem {
2+
private Map<Integer, String> checkInStation;
3+
private Map<Integer, Integer> checkInTime;
4+
private Map<String, int[]> totalTime;
5+
6+
public UndergroundSystem() {
7+
checkInStation = new HashMap<>();
8+
checkInTime = new HashMap<>();
9+
totalTime = new HashMap<>();
10+
}
11+
12+
public void checkIn(int id, String stationName, int t) {
13+
checkInStation.put(id, stationName);
14+
checkInTime.put(id, t);
15+
}
16+
17+
public void checkOut(int id, String stationName, int t) {
18+
int cost = t - checkInTime.remove(id);
19+
String startStation = checkInStation.remove(id);
20+
String stations = startStation + "." + stationName;
21+
int[] times = totalTime.getOrDefault(stations, new int[2]);
22+
times[0] += cost;
23+
++times[1];
24+
totalTime.put(stations, times);
25+
}
26+
27+
public double getAverageTime(String startStation, String endStation) {
28+
String stations = startStation + "." + endStation;
29+
int[] times = totalTime.get(stations);
30+
return times[0] * 1.0 / times[1];
31+
}
32+
}
33+
34+
/**
35+
* Your UndergroundSystem object will be instantiated and called as such:
36+
* UndergroundSystem obj = new UndergroundSystem();
37+
* obj.checkIn(id,stationName,t);
38+
* obj.checkOut(id,stationName,t);
39+
* double param_3 = obj.getAverageTime(startStation,endStation);
40+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class UndergroundSystem:
2+
3+
def __init__(self):
4+
self.check_in_station = {}
5+
self.check_in_time = {}
6+
self.total_time = {}
7+
8+
def checkIn(self, id: int, stationName: str, t: int) -> None:
9+
self.check_in_station[id] = stationName
10+
self.check_in_time[id] = t
11+
12+
def checkOut(self, id: int, stationName: str, t: int) -> None:
13+
cost = t - self.check_in_time.pop(id)
14+
start_station = self.check_in_station.pop(id)
15+
stations = start_station + '.' + stationName
16+
times = self.total_time.get(stations, [0, 0])
17+
times[0] += cost
18+
times[1] += 1
19+
self.total_time[stations] = times
20+
21+
def getAverageTime(self, startStation: str, endStation: str) -> float:
22+
stations = startStation + '.' + endStation
23+
times = self.total_time[stations]
24+
return times[0] / times[1]
25+
26+
27+
# Your UndergroundSystem object will be instantiated and called as such:
28+
# obj = UndergroundSystem()
29+
# obj.checkIn(id,stationName,t)
30+
# obj.checkOut(id,stationName,t)
31+
# param_3 = obj.getAverageTime(startStation,endStation)

0 commit comments

Comments
 (0)