1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (English)/最近的请求次数(English) [number-of-recent-calls].html

38 lines
1.8 KiB
HTML

<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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input</strong>
[&quot;RecentCounter&quot;, &quot;ping&quot;, &quot;ping&quot;, &quot;ping&quot;, &quot;ping&quot;]
[[], [1], [100], [3001], [3002]]
<strong>Output</strong>
[null, 1, 2, 3, 3]
<strong>Explanation</strong>
RecentCounter recentCounter = new RecentCounter();
recentCounter.ping(1); // requests = [<u>1</u>], range is [-2999,1], return 1
recentCounter.ping(100); // requests = [<u>1</u>, <u>100</u>], range is [-2900,100], return 2
recentCounter.ping(3001); // requests = [<u>1</u>, <u>100</u>, <u>3001</u>], range is [1,3001], return 3
recentCounter.ping(3002); // requests = [1, <u>100</u>, <u>3001</u>, <u>3002</u>], range is [2,3002], return 3
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= t &lt;= 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>