Class RecentCounter
java.lang.Object
g0901_1000.s0933_number_of_recent_calls.RecentCounter
933 - Number of Recent Calls.<p>Easy</p>
<p>You have a <code>RecentCounter</code> class which counts the number of recent requests within a certain time frame.</p>
<p>Implement the <code>RecentCounter</code> class:</p>
<ul>
<li><code>RecentCounter()</code> Initializes the counter with zero recent requests.</li>
<li><code>int ping(int t)</code> Adds a new request at time <code>t</code>, where <code>t</code> represents some time in milliseconds, and returns the number of requests that has happened in the past <code>3000</code> milliseconds (including the new request). Specifically, return the number of requests that have happened in the inclusive range <code>[t - 3000, t]</code>.</li>
</ul>
<p>It is <strong>guaranteed</strong> that every call to <code>ping</code> uses a strictly larger value of <code>t</code> than the previous call.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input</strong> [“RecentCounter”, “ping”, “ping”, “ping”, “ping”] [ [], [1], [100], [3001], [3002]]</p>
<p><strong>Output:</strong> [null, 1, 2, 3, 3]</p>
<p><strong>Explanation:</strong> RecentCounter recentCounter = new RecentCounter(); recentCounter.ping(1); // requests = [1], range is [-2999,1], return 1 recentCounter.ping(100); // requests = [1, 100], range is [-2900,100], return 2 recentCounter.ping(3001); // requests = [1, 100, 3001], range is [1,3001], return 3 recentCounter.ping(3002); // requests = [1, 100, 3001, 3002], range is [2,3002], return 3</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= t <= 10<sup>9</sup></code></li>
<li>Each test case will call <code>ping</code> with <strong>strictly increasing</strong> values of <code>t</code>.</li>
<li>At most <code>10<sup>4</sup></code> calls will be made to <code>ping</code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
RecentCounter
public RecentCounter()
-
-
Method Details
-
ping
public int ping(int t)
-