Leetcode—1396. 设计地铁系统【中等】

发布于:2024-05-06 ⋅ 阅读:(27) ⋅ 点赞:(0)

2024每日刷题(127)

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);
 */

运行结果

在这里插入图片描述
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!