[LeetCode] 933. Number of Recent Calls 最近的调用次数

Write a class?RecentCounter?to count recent requests.

It has only one method:?ping(int t), where t represents some time in milliseconds.

Return the number of?pings that have been made from 3000 milliseconds ago until now.

Any ping with time in?[t - 3000, t]?will count, including the current ping.

It is guaranteed that every call to?ping?uses a strictly larger value of?t?than before.

Example 1:

Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]
Output: [null,1,2,3,3]

Note:

  1. Each test case will have at most?10000?calls to?ping.
  2. Each test case will call?ping?with strictly increasing values of?t.
  3. Each call to ping will have?1 <= t <= 10^9.

这道题让实现一个 RecentCounter 类,里面有一个 ping 函数,输入给定了一个时间t,让我们求在 [t-3000, t] 时间范围内有多少次 ping。题目中限定了每次的给的时间一定会比上一次的时间大,而且只关心这个大小为 3001 的时间窗口范围内的次数,则利用滑动窗口 Sliding Window 来做就是个很不错的选择。由于数字是不断加入的,可以使用一个 queue,每当要加入一个新的时间点t时,先从队列开头遍历,若前面的时间不在当前的时间窗口内,则移除队列。之后再将当前时间点t加入,并返回队列的长度即可,参见代码如下:

class RecentCounter {
public:
    RecentCounter() {}

    int ping(int t) {
        while (!q.empty()) {
            if (q.front() + 3000 >= t) break;
            q.pop();
        }
        q.push(t);
        return q.size();
    }

private:
    queue<int> q;
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/933

参考资料:

https://leetcode.com/problems/number-of-recent-calls/

https://leetcode.com/problems/number-of-recent-calls/discuss/189334/C%2B%2B-Easy-and-Clean-solution-using-queue

https://leetcode.com/problems/number-of-recent-calls/discuss/189239/JavaPython-3-Five-solutions%3A-TreeMap-TreeSet-ArrayList-Queue-Circular-List.

LeetCode All in One 题目讲解汇总(持续更新中...)

原文地址:https://www.cnblogs.com/grandyang/p/12359774.html

时间: 2024-10-30 07:06:15

[LeetCode] 933. Number of Recent Calls 最近的调用次数的相关文章

leetcode 933. Number of Recent Calls

class RecentCounter { Queue<Integer> q; public RecentCounter() { q = new LinkedList<Integer>(); } public int ping(int t) { int threshold = t - 3000; while (q.size() != 0 && q.peek() < threshold) { q.poll(); } q.add(t); return q.size

933. Number of Recent Calls

933. Number of Recent Calls 写一个RecentCounter类来计算最近的请求. 它只有一个方法:ping(int t),其中,t代表以毫秒为单位的某个时间. 返回从3000毫秒前到现在的ping数. 任何处于 [t-3000,t] 时间范围之内的ping都会被计算在内,包括当前(指t时刻)的ping. 保证每次对ping的调用都使用比之前更大的t值. 示例: 输入:inputs = ["RecentCounter","ping",&qu

LeetCode.933-最近通话次数(Number of Recent Calls)

这是悦乐书的第357次更新,第384篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第219题(顺位题号是933).写一个类RecentCounter来计算最近的请求. 它只有一个方法:ping(int t),其中t代表一些时间(以毫秒为单位). 返回从3000毫秒前到现在为止的ping数. 在[t-3000,t]中任何时间ping都将计数,包括当前ping. 每次调用ping都使用比之前严格更大的t值.例如: 输入:inputs = ["RecentCounter&

[Swift Weekly Contest 109]LeetCode933. 最近的请求次数 | Number of Recent Calls

Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t represents some time in milliseconds. Return the number of pings that have been made from 3000 milliseconds ago until now. Any ping with time in [t - 3

[leetcode]Valid Number @ Python

原题地址:http://oj.leetcode.com/problems/valid-number/ 题意:判断输入的字符串是否是合法的数. 解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅.本文参考了http://blog.csdn.net/kenden23/article/details/18696083里面的内容,在此致谢! 首先这个题有9种状态: 0初始无输入或者只有space的状态1输入了数字之后的状态2前面无数字,只输入了dot的状态3输入了符号状态4前面有数字和有do

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

[leetcode]_Palindrome Number

判断integer是否为回文串(负数全部不为回文串) 思路很直接,提取出integer中的每一位,一头一尾进行比较是否相同. 一次AC , 直接上代码: public boolean isPalindrome(int x) { if(x < 0) return false; else if(x >= 0 && x <= 9) return true; else{ int[] num = new int[20]; int i = 0 ; while(x > 0){ n

LeetCode: Single Number Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r