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”,“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]范围,最会返回List的size即可。
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)的更多相关文章
- [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 ...
- 【Leetcode_easy】933. Number of Recent Calls
problem 933. Number of Recent Calls 参考 1. Leetcode_easy_933. Number of Recent Calls; 完
- [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 ...
- 【LeetCode】933. Number of Recent Calls 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 队列 相似题目 参考资料 日期 题目地址: ...
- C#LeetCode刷题之#933-最近的请求次数(Number of Recent Calls)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4134 访问. 写一个 RecentCounter 类来计算最近的 ...
- LeetCode - Number of Recent Calls
Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...
- 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 ...
- LeetCode:最少移动次数使得数组元素相等||【462】
LeetCode:最少移动次数使得数组元素相等||[462] 题目描述 给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最 ...
- LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))
LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过 ...
随机推荐
- Kendo UI for jQuery自定义小部件第一弹!不得不看的入门指南
Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...
- 基于idea的maven(一)Maven的安装
1.Maven前置依赖 检查电脑是是否安装java 2.下载maven 网址 www.apache.org 解压 maven 压缩包, 并创建相应的maven本地仓库的路径. 打开 conf文件夹中 ...
- php和http协议
http协议:电脑与电脑,网络与网络之间传输需要的一些条件: 比如网线互联,能相互找到ip地址(tcp/ip: b/s结构一定要遵循http协议): 报文都要有报头,要给谁传数据,要传什么数据,传什么 ...
- AXIOS构建请求处理全局loading状态&&AXIOS避免重复请求loading多次出现
一般情况下,在 vue 中结合 axios 的拦截器控制 loading 展示和关闭,是这样的:在 App.vue 配置一个全局 loading. <div class="app&qu ...
- Linux 系统中 grep 的ABC参数含义
1.grep -A 5 匹配行及后5行 2.grep -B 5 匹配行及前5行 3.grep -C 5 匹配行及前后各5行
- 并行计算基础(1)(GPU架构介绍)
一.常用术语 Task:任务.可以完整得到结果的一个程序,一个程序段或若干个程序段.例如搬砖. Parallel Task:并行任务.可以并行计算的任务.多个人搬砖. Serial Execution ...
- hdu 5695 百度熊教体育 拓扑排序 好题
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 论文阅读:Camdoop: Exploiting In-network Aggregation for Big Data Applications
摘要: 大公司与中小型企业每天都在批处理作业和实时应用程序中处理大量数据,这会产生大量的网络流量,而使用传统的的网络基础架构则很难支持.为了解决这个问题已经提出了几种新颖的网络拓扑,旨在增加企业集群中 ...
- EL表达式无效问题
引起原因web.xml中: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...
- Uep的ajaxform和ajaxgrid组件获取数据源
对于ajaxform组件var record = ajaxform.getRecord();var storeId = record.get("storeId");var stor ...