Skip to content

Commit

Permalink
Time: 152 ms (84.99%), Space: 59.3 MB (34.65%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
jitender1222 committed Apr 24, 2022
1 parent 4282279 commit 543dda5
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 1396-design-underground-system/1396-design-underground-system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class UndergroundSystem {
public:
unordered_map<string, pair<double, int>> avg; // travelName => {overallTime,numberOfTravels}

unordered_map<int, pair<string, int>>checkin; // id => {entryStation, entryTime}
UndergroundSystem() {

}

void checkIn(int id, string stationName, int t) {
checkin[id] = {stationName, t};
}

void checkOut(int id, string stationName, int t) {
string str = checkin[id].first + "->" + stationName;
double time = t - checkin[id].second;
avg[str].first += time;
avg[str].second++;
}

double getAverageTime(string startStation, string endStation) {
string s = startStation + "->" + endStation;
return avg[s].first / avg[s].second;
}
};

/**
* Your UndergroundSystem object will be instantiated and called as such:
* UndergroundSystem* obj = new UndergroundSystem();
* obj->checkIn(id,stationName,t);
* obj->checkOut(id,stationName,t);
* double param_3 = obj->getAverageTime(startStation,endStation);
*/

0 comments on commit 543dda5

Please sign in to comment.