Number of Recent Calls Solutions in C++
Number 933
Difficulty Easy
Acceptance 71.9%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/number-of-recent-calls/// Author : Hao Chen// Date : 2020-07-26class RecentCounter {public:RecentCounter() {}int ping(int t) {req.push_back(t);return req.size() - binary_search(t-3000);}private:vector<int> req;int binary_search(int x) {int low=0, high=req.size()-1;while(low < high) {int mid = low + (high -low) / 2;if ( req[mid] == x ) return mid;if ( req[mid] < x ) low = mid + 1;else high = mid - 1;}cout << "x=" << x << "\tlow=" << low << endl;return x > req[low] ? low+1 : low ;}};/*** Your RecentCounter object will be instantiated and called as such:* RecentCounter* obj = new RecentCounter();* int param_1 = obj->ping(t);*/
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/number-of-recent-calls//// Author : liuyubobobo/// Time : 2018-11-03#include <iostream>#include <queue>using namespace std;/// Using a Queue/// Time Complexity: O(query)/// Space Complexity: O(3000)class RecentCounter {private:queue<int> q;public:RecentCounter() { }int ping(int t) {q.push(t);while(t - 3000 > q.front())q.pop();return q.size();}};int main() {return 0;}