问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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
3 1
2
3
2

分析:

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

C#LeetCode刷题之#933-最近的请求次数(Number of Recent Calls)的更多相关文章

  1. LeetCode.933-最近通话次数(Number of Recent Calls)

    这是悦乐书的第357次更新,第384篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第219题(顺位题号是933).写一个类RecentCounter来计算最近的请求. 它 ...

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

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

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

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

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

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

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

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

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

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

  7. C#LeetCode刷题-队列

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

  8. C#LeetCode刷题-位运算

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

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

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

随机推荐

  1. Ethical Hacking - GAINING ACCESS(21)

    CLIENT SIDE ATTACKS - Trojan delivery method - using email spoofing Use gathered info to contract ta ...

  2. Mobilenet V1

    目录 1. Depth Separable Convolution 2. 网络结构 3. 宽度因子和分辨率因子 4. 代码实现 参考博客: https://cuijiahua.com/blog/201 ...

  3. Shell基本语法---处理海量数据的grep命令

    grep命令 shell脚本三剑客之一 grep应用场景:通常对数据进行 行的提取 语法:grep [选项] [内容] [file] -v 对内容进行取反提取 -n 对提取的内容显示行号 -w 精确匹 ...

  4. CSS变形动画

    CSS变形动画 前言 在开始介绍CSS变形动画之前,可以先了解一下学习了它之后能做什么,有什么用,这样你看这篇文章可能会有一些动力. 学习了CSS变形动画后,你可以为你的页面做出很多炫酷的效果,如一个 ...

  5. DC-1靶机实战和分析

    前言 我们都知道,对靶机的渗透,可以宽阔自己的解题思路,练习并熟悉相关操作命令,提高自己的能力.下面我就对Vulnhub的DC-1靶机进行渗透,靶机设置了5个flag,咱们依次找到它.并通过图文形式讲 ...

  6. w10查看wifi密码

    1.选择网络和Internet设置 右键单击网络连接图标,选择“打开网络和Internet设置”. 2.选择网络和共享中心

  7. Eclipse创建Web项目后新建Servlet时报红叉错误 or 导入别人Web项目时报红叉错误 的解决办法

    如图,出现类似红叉错误. 1.在项目名称上点击右键->Build Path->Configure Build Path 2.在弹出来的框中点击Add Library,如图 3.接下来选择U ...

  8. 一个edit的学习笔记

    https://blog.csdn.net/woshizoe/article/details/51555396

  9. 动态页面技术(EL/JSTL)

    EL技术 EL 表达式概述 EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL出现的目的是要替代jsp页面中脚本(java代码)的编写. EL从域中取出数 ...

  10. MacOS下JDK8的安装与配置

    微信搜索"艺术行者",关注并回复关键词"jdk8"获取安装包和API文档资料! 一.安装环节 1.打开网页 https://www.oracle.com/jav ...