作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/number-of-recent-calls/description/

题目描述

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.

题目大意

找出最近的3000毫秒内有多少个调用请求。每个调用请求是ping(t)函数,其中t是请求的时间,可以保证每次ping的参数t是大于前面的。

解题方法

二分查找

本周周赛第一题,我是用二分查找快速写出了这个题的解法,为后面的题省下了不少时间。

二分的想法是,我找到小于t-3000时间的元素索引,那么总共有多少个元素就是总长度-小于t-3000时间的元素索引。题目严格递增这个描述直接告诉了我们可以只用二分。二分使用的是bisect的bisect_left(),这个给我们返回的是小于目标元素的第一个结果。

时间复杂度是O(t*log(t)),空间复杂度O(t).

class RecentCounter:

    def __init__(self):
self.nums = [] def ping(self, t):
"""
:type t: int
:rtype: int
"""
self.nums.append(t)
cur_pos = len(self.nums)
prev_pos = bisect.bisect_left(self.nums, t - 3000)
return cur_pos - prev_pos # Your RecentCounter object will be instantiated and called as such:
# obj = RecentCounter()
# param_1 = obj.ping(t)

队列

这个解法我是看了discuss才想到的,这个方法使用一个队列,当t时间到达之后,在t-3000之前的调用全部删除,因为这些不会对后面的产生任何影响了。删除之后,求长度就好了。

时间复杂度是O(t),空间复杂度O(t).比上面快一点。

class RecentCounter:

    def __init__(self):
self.que = collections.deque() def ping(self, t):
"""
:type t: int
:rtype: int
"""
while self.que and self.que[0] < t - 3000:
self.que.popleft()
self.que.append(t)
return len(self.que) # Your RecentCounter object will be instantiated and called as such:
# obj = RecentCounter()
# param_1 = obj.ping(t)

相似题目

参考资料

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

日期

2018 年 11 月 4 日 —— 下雨的周日

最新文章

  1. Ruby安装Scss
  2. ContentControl与ContentPresenter区别?
  3. 分页查询的SQL语句
  4. DSP using MATLAB 示例Example2.4
  5. symfony中twig的模板载入
  6. 提高Linux上socket性能
  7. jps
  8. python学习笔记5(元组)
  9. JEECG开发总结
  10. 飞天熊猫游戏源代码android文本
  11. Merge into的使用详解-你Merge了没有
  12. 消消乐、candy crush类三消游戏程序逻辑分析
  13. OpenStack(企业私有云)万里长征第二步——使用Fuel部署
  14. .Net Core 部署到IIS
  15. Torch功能点记录
  16. 几何入门合集 gym101968 problem F. Mirror + gym102082 Problem F Fair Chocolate-Cutting + gym101915 problem B. Ali and Wi-Fi
  17. hadoop基础与实践--流程解惑
  18. 深入浅出Redis05-Redis集群环境的配置
  19. DRF 分页组件
  20. nodejs通过request请求远程url的文件并下载到本地

热门文章

  1. Zabbix源码安装,使用service命令管理zabbix进程
  2. EXCEL-COUNTIF()统计符合区间上的值个数
  3. C/C++运行时确定字节顺序
  4. 非寻常方式学习ApacheTomcat架构及10.0.12源码编译
  5. android Paint 详解
  6. 一份不错的Java就业指导
  7. Ruby Gems更换淘宝源方法
  8. 类型类 &amp;&amp; .class 与 .getClass() 的区别
  9. 一文读懂RESTful架构
  10. js和jquery之间的转换