|
| 1 | +<h2><a href="https://leetcode.com/problems/number-of-recent-calls">969. Number of Recent Calls</a></h2><h3>Easy</h3><hr><p>You have a <code>RecentCounter</code> class which counts the number of recent requests within a certain time frame.</p> |
| 2 | + |
| 3 | +<p>Implement the <code>RecentCounter</code> class:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li><code>RecentCounter()</code> Initializes the counter with zero recent requests.</li> |
| 7 | + <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> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<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> |
| 11 | + |
| 12 | +<p> </p> |
| 13 | +<p><strong class="example">Example 1:</strong></p> |
| 14 | + |
| 15 | +<pre> |
| 16 | +<strong>Input</strong> |
| 17 | +["RecentCounter", "ping", "ping", "ping", "ping"] |
| 18 | +[[], [1], [100], [3001], [3002]] |
| 19 | +<strong>Output</strong> |
| 20 | +[null, 1, 2, 3, 3] |
| 21 | + |
| 22 | +<strong>Explanation</strong> |
| 23 | +RecentCounter recentCounter = new RecentCounter(); |
| 24 | +recentCounter.ping(1); // requests = [<u>1</u>], range is [-2999,1], return 1 |
| 25 | +recentCounter.ping(100); // requests = [<u>1</u>, <u>100</u>], range is [-2900,100], return 2 |
| 26 | +recentCounter.ping(3001); // requests = [<u>1</u>, <u>100</u>, <u>3001</u>], range is [1,3001], return 3 |
| 27 | +recentCounter.ping(3002); // requests = [1, <u>100</u>, <u>3001</u>, <u>3002</u>], range is [2,3002], return 3 |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong>Constraints:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li><code>1 <= t <= 10<sup>9</sup></code></li> |
| 35 | + <li>Each test case will call <code>ping</code> with <strong>strictly increasing</strong> values of <code>t</code>.</li> |
| 36 | + <li>At most <code>10<sup>4</sup></code> calls will be made to <code>ping</code>.</li> |
| 37 | +</ul> |
0 commit comments