From a825b6b51ff6a5cc24fc2ecdfcd2659ff3c048c2 Mon Sep 17 00:00:00 2001 From: eat-swap Date: Fri, 6 May 2022 15:34:55 +0800 Subject: [PATCH] add: 220506-CN [cpp] --- cpp/2205/220506-CN.cpp | 26 ++++++++++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220506-CN.cpp diff --git a/cpp/2205/220506-CN.cpp b/cpp/2205/220506-CN.cpp new file mode 100644 index 0000000..9419334 --- /dev/null +++ b/cpp/2205/220506-CN.cpp @@ -0,0 +1,26 @@ +#include + +/** + * 933. Number of Recent Calls + * You have a RecentCounter class which counts the number of recent requests within a certain time frame. + * + * Implement the RecentCounter class: + * + * RecentCounter() Initializes the counter with zero recent requests. + * int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range [t - 3000, t]. + * It is guaranteed that every call to ping uses a strictly larger value of t than the previous call. + */ + +class RecentCounter { +private: + std::queue q; +public: + RecentCounter() = default; + + int ping(int t) { + q.push(t); + while (q.front() < t - 3000) + q.pop(); + return q.size(); + } +}; diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index d038401..d140727 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220506.cpp) +ADD_EXECUTABLE(2205 220506-CN.cpp)