这是悦乐书的第357次更新,第384篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第219题(顺位题号是933)。写一个类RecentCounter来计算最近的请求。

它只有一个方法:ping(int t),其中t代表一些时间(以毫秒为单位)。

返回从3000毫秒前到现在为止的ping数。

[t-3000,t]中任何时间ping都将计数,包括当前ping

每次调用ping都使用比之前严格更大的t值。例如:

输入:inputs = [“RecentCounter”,“ping”,“ping”,“ping”,“ping”],
inputs = [[],[1],[100],[3001],[3002]]
输出:[null,1,2,3,3]

注意

  • 每个测试用例最多可以有10000次ping操作。

  • 每个测试用例都会严格增加t的值来调用ping。

  • 每次调用ping,t的取值范围为1 <= t <= 10^9。

02 第一种解法

题目的意思是每次调用RecentCounter类的ping方法时,计算[t-3000,t]范围内的数有多少,而t每次都会增加,也就是说,由多次调用ping方法组成的t数组,是一个递增数组,而我们只需要每次拿到新的t时,计算[t-3000,t]内的t有多少个。

使用List存储每次调用ping方法时传入的t,然后计数[t-3000,t]内的元素个数。

class RecentCounter {

    List<Integer> list;

    public RecentCounter() {
list = new ArrayList<Integer>();
} public int ping(int t) {
list.add(t);
int min = t-3000, count = 0;
for (Integer num : list) {
if (num >= min && num <= t) {
count++;
}
}
return count;
}
} /**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/

03 第二种解法

同样的思路,但是要比上面的解法更加高效。

因为t是一个递增的数,并且每次计算时,新的t是肯定包含在其中的,所以我们只需要判断[t-3000,t]中的前半部分t-3000即可,从List的第一位元素开始,如果小于t-3000就移除,直到List中的第一位元素符合[t-3000,t]范围,最会返回Listsize即可。

class RecentCounter {

    List<Integer> list;

    public RecentCounter() {
list = new ArrayList<Integer>();
} public int ping(int t) {
list.add(t);
int i = 0, n = list.size();
while (i < n && list.get(i) < t-3000) {
list.remove(list.get(i));
}
return list.size();
}
} /**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/

04 第三种解法

从第二种解法中,可以看出t数组是存在一种先后顺序,因为我们永远只需要处理前面先进来的数据,而这一特性,可以联想到队列,因为他们都有先进先出的特性。

每次调用ping方法时,将t入队列,然后判断队列顶部的元素是否小于t-3000,如果小于,就将队列顶部的元素移除,直到队列顶部元素大于等于t-3000,最后返回队列的size即可。

class RecentCounter {

    Queue<Integer> queue;

    public RecentCounter() {
queue = new LinkedList<Integer>();
} public int ping(int t) {
if (queue.isEmpty()) {
queue.offer(t);
return 1;
} else {
int min = t-3000;
while (!queue.isEmpty() && queue.peek() < min) {
queue.poll();
}
queue.offer(t);
}
return queue.size();
}
} /**
* Your RecentCounter object will be instantiated and called as such:
* RecentCounter obj = new RecentCounter();
* int param_1 = obj.ping(t);
*/

05 小结

算法专题目前已连续日更超过六个月,算法题文章225+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode.933-最近通话次数(Number of Recent Calls)的更多相关文章

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

    Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...

  2. 【Leetcode_easy】933. Number of Recent Calls

    problem 933. Number of Recent Calls 参考 1. Leetcode_easy_933. Number of Recent Calls; 完

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

    Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...

  4. 【LeetCode】933. Number of Recent Calls 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 队列 相似题目 参考资料 日期 题目地址: ...

  5. C#LeetCode刷题之#933-最近的请求次数(Number of Recent Calls)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4134 访问. 写一个 RecentCounter 类来计算最近的 ...

  6. LeetCode - Number of Recent Calls

    Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...

  7. 109th LeetCode Weekly Contest Number of Recent Calls

    Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...

  8. LeetCode:最少移动次数使得数组元素相等||【462】

    LeetCode:最少移动次数使得数组元素相等||[462] 题目描述 给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最 ...

  9. LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

    LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...

随机推荐

  1. qa角色记一次测试过程回溯

    一.测试过程简述 a项目依赖b项目新功能,ab项目一起提测 1.测试人员:两老一新 2.测试过程:第一轮,三人执行用例 第二轮,三人各自模块发散 第三轮,三人交叉测试 第四轮,两老投入b项目性能以及接 ...

  2. inode、软硬链接

    关于inode是什么,可以看这篇文章:http://www.cnblogs.com/adforce/p/3522433.html 如何查看inode ll -di /boot / /app查看文件和文 ...

  3. win7系统安装虚拟网卡进行与Linux虚拟机的通信

    有网络情况下,安装Linux时选择网桥即可实现Window与Linux直接通信. 无网络情况下,最简单的方法是在window系统中安装虚拟网卡,以进行与Linux的通信,步骤如下: (1)右击“我的计 ...

  4. Phaserjs怎样用ES6开发游戏

    想用ES6语法开发phaserjs游戏,是phaserCE,但是不知道怎么导入,总是报错,后来经过多次尝试,解决方法如下: 干脆不导入,直接暴露到window里,然后模块化的代码全部在window.o ...

  5. 吴恩达+neural-networks-deep-learning+第二周作业

    Logistic Regression with a Neural Network mindset v4 简单用logistic实现了猫的识别,logistic可以被看做一个简单的神经网络结构,下面是 ...

  6. mvc api 关于 post 跟get 请求的一些想法[FromUri] 跟[FromBody] 同一个控制器如何实现共存

    wep api  在设置接收请求参数的时候,会自动根据模型进行解析. [FromUrl] 跟[FromBody] 不可以同时使用. 要拆分开: [HttpGet] public object GetP ...

  7. shell学习——关于shell函数库的使用

    shell函数库的理解: 个人理解,shell函数库实质为一个脚本,脚本内包含了多个函数(函数具有普遍适用性). shell函数库的调用: 通过  . /path/lib/file.lib 或者 so ...

  8. 18.configparser模块

    # 创建配置文件 import configparser config = configparser.ConfigParser() # 相当于config = {} 空字典 config[" ...

  9. Java中 DecimalFormat 用法详解

    我们经常要将数字进行格式化,比如取2位小数,这是最常见的.Java 提供DecimalFormat类,帮你用最快的速度将数字格式化为你需要的样子.下面是一个例子: import java.text.D ...

  10. Cobbler自动装机

    preface 我们之前批量安装操作系统的时候都是采用pxe来安装,pxe也是通过网络安装操作系统的,但是PXE依赖于DHCP,HTTP/TFTP,kicstart等支持.安装流程如下所示: 对于上面 ...