问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4134 访问。

写一个 RecentCounter 类来计算最近的请求。

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

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

任何处于 [t - 3000, t] 时间范围之内的 ping 都将会被计算在内,包括当前(指 t 时刻)的 ping。

保证每次对 ping 的调用都使用比之前更大的 t 值。

输入:inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]

输出:[null,1,2,3,3]

提示:

  • 每个测试用例最多调用 10000 次 ping。
  • 每个测试用例会使用严格递增的 t 值来调用 ping。
  • 每次调用 ping 都有 1 <= t <= 10^9。

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.

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.

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4134 访问。

public class Program {public static void Main(string[] args) {var count = new RecentCounter();Console.WriteLine(count.Ping(1));Console.WriteLine(count.Ping(100));Console.WriteLine(count.Ping(3001));Console.WriteLine(count.Ping(3002));Console.WriteLine();var count2 = new RecentCounter2();Console.WriteLine(count2.Ping(1));Console.WriteLine(count2.Ping(100));Console.WriteLine(count2.Ping(3000));Console.WriteLine(count2.Ping(5000));Console.ReadKey();}public class RecentCounter {public RecentCounter() {_list = new List<int>();}private List<int> _list = null;public int Ping(int t) {//列表法,思路简单暴力_list.Add(t);var last = _list[_list.Count - 1];var count = 1;for(var i = _list.Count - 2; i >= 0; i--) {if(last - _list[i] <= 3000) {count++;} else {break;}}return count;}}public class RecentCounter2 {public RecentCounter2() {_queue = new Queue<int>();}private Queue<int> _queue = null;public int Ping(int t) {//队列法_queue.Enqueue(t);//干掉时间差大于 3000 的,它们没有必要参与运算while(t - _queue.Peek() > 3000) _queue.Dequeue();return _queue.Count;}}}

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4134 访问。

1
2
3
31
2
3
2

分析:

显而易见,以上2种算法的时间复杂度均为:  。

C#LeetCode刷题之#933-最近的请求次数(Number of Recent Calls)相关推荐

  1. C#LeetCode刷题之#374-猜数字大小(Guess Number Higher or Lower)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3993 访问. 我们正在玩一个猜数字游戏. 游戏规则如下: 我从 ...

  2. C#LeetCode刷题之#202-快乐数(Happy Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3856 访问. 编写一个算法来判断一个数是不是"快乐数& ...

  3. C#LeetCode刷题之#507-完美数(Perfect Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3879 访问. 对于一个 正整数,如果它和除了它自身以外的所有正因 ...

  4. C#LeetCode刷题之#9-回文数(Palindrome Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3840 访问. 判断一个整数是否是回文数.回文数是指正序(从左向右 ...

  5. C#LeetCode刷题之#268-缺失数字(Missing Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4056 访问. 给定一个包含 0, 1, 2, ..., n 中  ...

  6. C#LeetCode刷题-队列

    队列篇 # 题名 刷题 通过率 难度 363 矩形区域不超过 K 的最大数值和 27.2% 困难 621 任务调度器 40.9% 中等 622 设计循环队列 C#LeetCode刷题之#622-设计循 ...

  7. C#LeetCode刷题-位运算

    位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 1 ...

  8. C#LeetCode刷题-二分查找​​​​​​​

    二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

  9. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 (a ...

最新文章

  1. 前端每日实战:93# 视频演示如何用纯 CSS 创作一根闪电连接线
  2. VBS字符编码的说明
  3. Community Server :: Forums
  4. intellij idea 1314 插件推荐及快速上手建议 (已更新!)
  5. android 权限管理框架,Android 运行时权限管理最佳实践
  6. 怎么在电脑安装php文件夹在哪个文件夹,php进行文件上传时找不到临时文件夹怎么办,电脑自动保存的文件在哪里...
  7. android导入项目出现style错误,menu错误
  8. 单例模式的练习-如何正确构建
  9. 聊聊时间管理,不是多人运动那种
  10. 如何获得完美的调色板?完美的配色素材专辑拿走!
  11. IO流-ReadLine方法的原理 自定义BufferedReader
  12. SAP License:数据导入时的问题与总结
  13. CAD复制,如何自由复制CAD图形?
  14. miui8 android版本,miui8官方正式版下载_小米8系统安卓下载|好特下载
  15. JavaScript快速入门
  16. 最近很火的京东、天猫超市飞天茅台抢购是怎么回事,从原理流程给你们分析一波
  17. Brocade switch upgrade firmware
  18. 程序员如何保护自己的颈椎?颈椎操
  19. Illegal invocation 解决方法
  20. 用Photoshop将照片卡通化

热门文章

  1. 【Linux】生产者消费者编程实现-线程池+信号量
  2. 创建线程方式二 java 1615474026
  3. 记单词工具 百词斩 0124
  4. 格式 数组的基本使用 0912
  5. 练习-前程无忧数据爬取
  6. linux-tar压缩与解压缩
  7. linux 环境下 openssl 生成ecdsa公、私钥
  8. 重写慢日志解析程序,实现打印慢SQL信息及其所属数据库
  9. iview 3.x 升级指南 —— Icon 篇
  10. Windows Azure 部署 Windows 8 虚拟机