Leetcode—1396. 设计地铁系统【中等】
class UndergroundSystem {
public:
typedef struct Checkin {
string startStation;
int time;
} Checkin;
typedef struct Checkout{
int tripNum;
int totalTime;
} Checkout;
UndergroundSystem() {
}
void checkIn(int id, string stationName, int t) {
CheckinPerson[id] = {stationName, t};
}
void checkOut(int id, string stationName, int t) {
auto [startStation, time] = CheckinPerson[id];
string name = startStation + "->" + stationName;
stationInfo[name].tripNum++;
stationInfo[name].totalTime += t - time;
CheckinPerson.erase(id);
}
double getAverageTime(string startStation, string endStation) {
auto [tripNum, totalTime] = stationInfo[startStation + "->" + endStation];
if(tripNum == 0) {
return 0;
}
return totalTime / (double)tripNum;
}
private:
unordered_map<int, Checkin> CheckinPerson;
unordered_map<string, Checkout> stationInfo;
};
/**
* 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);
*/