Skip to content

Commit d524abf

Browse files
authored
Create design-underground-system.cpp
1 parent f1605ec commit d524abf

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

C++/design-underground-system.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class UndergroundSystem {
5+
public:
6+
UndergroundSystem() {
7+
8+
}
9+
10+
void checkIn(int id, string stationName, int t) {
11+
live_[id] = {stationName, t};
12+
}
13+
14+
void checkOut(int id, string stationName, int t) {
15+
const auto [startStation, startTime] = move(live_[id]); live_.erase(id);
16+
statistics_[startStation][stationName].first += t - startTime;
17+
++statistics_[startStation][stationName].second;
18+
}
19+
20+
double getAverageTime(string startStation, string endStation) {
21+
const auto& [numer, denom] = statistics_[startStation][endStation];
22+
return double(numer) / denom;
23+
}
24+
25+
private:
26+
unordered_map<int, pair<string, int>> live_;
27+
unordered_map<string, unordered_map<string, pair<int, int>>> statistics_;
28+
};

0 commit comments

Comments
 (0)