-
Notifications
You must be signed in to change notification settings - Fork 9
/
UndergroundSystem.cpp
50 lines (41 loc) · 1.54 KB
/
UndergroundSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Problem: https://leetcode.com/problems/design-underground-system/
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
/**
* 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);
*/
class UndergroundSystem {
public:
struct CheckInInfo {
CheckInInfo() : stationName(""), timestamp(-1) {}
CheckInInfo(string s, int t) : stationName(s), timestamp(t) {}
string stationName;
int timestamp;
};
UndergroundSystem() {}
void checkIn(int id, string stationName, int t) {
CheckInInfo cinfo = CheckInInfo(stationName, t);
check_in_map_[id] = cinfo;
}
void checkOut(int id, string stationName, int t) {
CheckInInfo cinfo = check_in_map_.at(id);
times_map_[cinfo.stationName][stationName].push_back(t - cinfo.timestamp);
check_in_map_.erase(id);
}
double getAverageTime(string startStation, string endStation) {
vector<int> deltas = (times_map_.at(startStation)).at(endStation);
int sum = 0;
for (int delta : deltas) { sum += delta; }
double average = ((double)sum)/deltas.size();
return average;
}
private:
unordered_map<string, unordered_map<string, vector<int>>> times_map_;
unordered_map<int, CheckInInfo> check_in_map_;
};