Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 69d7de0

Browse filesBrowse files
authored
Create 20 designUndergroundSystem.cpp
1 parent c18073f commit 69d7de0
Copy full SHA for 69d7de0

File tree

Expand file treeCollapse file tree

1 file changed

+40
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+40
-0
lines changed
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class UndergroundSystem {
2+
// id => entryStation
3+
string checkInName[1000001];
4+
// id => entryTime
5+
int checkInTime[1000001];
6+
// id => {numberOfTravels, overallTime}
7+
unordered_map<string, pair<int, int>> avgTimesMap;
8+
public:
9+
UndergroundSystem() {
10+
// cout << "I am a constructor and I am as useless as \"ueue\" in \"queue\"\n";
11+
}
12+
13+
void checkIn(int id, string stationName, int t) {
14+
checkInName[id] = stationName;
15+
checkInTime[id] = -t;
16+
}
17+
18+
void checkOut(int id, string stationName, int t) {
19+
// retrieving matching checkin data
20+
int travelTime = checkInTime[id] + t;
21+
// computing travelName id
22+
string travelName = checkInName[id] + stationName;
23+
// updating data on the current travel
24+
auto currTravel = avgTimesMap.find(travelName);
25+
if (currTravel == end(avgTimesMap)) avgTimesMap[travelName] = {1, travelTime};
26+
else {
27+
currTravel->second.first++;
28+
currTravel->second.second += travelTime;
29+
}
30+
}
31+
32+
double getAverageTime(string startStation, string endStation) {
33+
// computing travelName id
34+
string travelName = startStation + endStation;
35+
// retrieving data on current travel
36+
auto currTravel = avgTimesMap[travelName];
37+
// returning the average time
38+
return currTravel.second / (double)currTravel.first;
39+
}
40+
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.