From 9dae32fd729a357e8401f4b4d0f67c8b954d3369 Mon Sep 17 00:00:00 2001 From: eat-swap Date: Sun, 24 Apr 2022 13:39:29 +0800 Subject: [PATCH] add: 220424 [cpp] --- cpp/2204/220424.cpp | 53 +++++++++++++++++++++++++++++++++++++++++ cpp/2204/CMakeLists.txt | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 cpp/2204/220424.cpp diff --git a/cpp/2204/220424.cpp b/cpp/2204/220424.cpp new file mode 100644 index 0000000..a7644d8 --- /dev/null +++ b/cpp/2204/220424.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +struct PairHash { + template + std::size_t operator()(const std::pair& p) const { + return std::hash()(p.first) ^ std::hash()(p.second); + } +}; + +/** + * 1396. Design Underground System + * An underground railway system is keeping track of customer travel times between different stations. They are using this data to calculate the average time it takes to travel from one station to another. + * + * Implement the UndergroundSystem class: + * + * void checkIn(int id, string stationName, int t) + * A customer with a card ID equal to id, checks in at the station stationName at time t. + * A customer can only be checked into one place at a time. + * void checkOut(int id, string stationName, int t) + * A customer with a card ID equal to id, checks out from the station stationName at time t. + * double getAverageTime(string startStation, string endStation) + * Returns the average time it takes to travel from startStation to endStation. + * The average time is computed from all the previous traveling times from startStation to endStation that happened directly, meaning a check in at startStation followed by a check out from endStation. + * The time it takes to travel from startStation to endStation may be different from the time it takes to travel from endStation to startStation. + * There will be at least one customer that has traveled from startStation to endStation before getAverageTime is called. + * You may assume all calls to the checkIn and checkOut methods are consistent. If a customer checks in at time t1 then checks out at time t2, then t1 < t2. All events happen in chronological order. + */ + +class UndergroundSystem { +private: + std::unordered_map> checkInTable; + std::unordered_map, std::pair, PairHash> travelTime; +public: + UndergroundSystem() = default; + + void checkIn(int id, const std::string& s, int t) { + checkInTable[id] = {t, s}; + } + + void checkOut(int id, const std::string& outSName, int tOut) { + const auto& [tIn, inSName] = checkInTable[id]; + auto& [cnt, totalTime] = travelTime[{inSName, outSName}]; + ++cnt; + totalTime += tOut - tIn; + } + + double getAverageTime(const std::string& startStation, const std::string& endStation) { + const auto& [cnt, total] = travelTime[{startStation, endStation}]; + return double(total) / cnt; + } +}; diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index 679d89a..2258f9e 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220423.cpp) +ADD_EXECUTABLE(2204 220424.cpp)