【LeetCode】933. Number of Recent Calls 解题报告(Python)
作者: 负雪明烛
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 ping
s 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:
- Each test case will have at most 10000 calls to ping.
- Each test case will call ping with strictly increasing values of t.
- 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 日 —— 下雨的周日
【LeetCode】933. Number of Recent Calls 解题报告(Python)的更多相关文章
- [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_easy】933. Number of Recent Calls
problem 933. Number of Recent Calls 参考 1. Leetcode_easy_933. Number of Recent Calls; 完
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
- 【LeetCode】729. My Calendar I 解题报告
[LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...
随机推荐
- Linux内网时钟同步问题(ntp和chrony)
我们都知道时钟同步可以使用外网服务器,在内网内不能连接外网的时候也需要时钟同步,那怎么进行呢? 选择内网的一台稳定的服务器作为时钟源,然后让其他机器都来同步这台机器即可. 注:其实ntp服务和chro ...
- Linux— file命令 用于辨识文件类型
Linux file命令用于辨识文件类型. 通过file指令,我们得以辨识该文件的类型. 语法 file [-bcLvz][-f <名称文件>][-m <魔法数字文件>...] ...
- 48-Merge Sorted Array
$88. Merge Sorted Array My Submissions QuestionEditorial Solution Total Accepted: 98885 Total Submis ...
- Elasticsearch中关于transform的一个问题?
背景:现在有一个业务,派件业务,业务员今天去派件(扫描产生一条派件记录),派件可能会有重复派件的情况,第二天再派送(记录被更新,以最新的派件操作为准).现在需要分业务员按天统计每天的派件数量.es版本 ...
- Netty之Channel*
Netty之Channel* 本文内容主要参考**<<Netty In Action>> ** 和Netty的文档和源码,偏笔记向. 先简略了解一下ChannelPipelin ...
- SpringBoot之HandlerInterceptorAdapter
SpringBoot之HandlerInterceptorAdapter 在SpringBoot中我们可以使用HandlerInterceptorAdapter这个适配器来实现自己的拦截器.这样就 ...
- android知识点duplicateParentState
android知识点duplicateParentState 今天要做一个效果,组件RelativeLayout上有两个TextView,这两个TextView具有不同的颜色值,现在要的效果是,当Re ...
- CentOS 初体验三: Yum 安装、卸载软件
一:Yum 简介 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指 ...
- When should we write our own copy constructor?
Reference:http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html Please write ...
- Spring Boot 和 Spring Cloud Feign调用服务及传递参数踩坑记录
背景 :在Spring Cloud Netflix栈中,各个微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnectio ...