Design a hit counter which counts the number of hits received in the past 5 minutes.

Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made to the system in chronological order (ie, the timestamp is monotonically increasing). You may assume that the earliest timestamp starts at 1.

It is possible that several hits arrive roughly at the same time.

Example:
HitCounter counter = new HitCounter(); // hit at timestamp 1.
counter.hit(1); // hit at timestamp 2.
counter.hit(2); // hit at timestamp 3.
counter.hit(3); // get hits at timestamp 4, should return 3.
counter.getHits(4); // hit at timestamp 300.
counter.hit(300); // get hits at timestamp 300, should return 4.
counter.getHits(300); // get hits at timestamp 301, should return 3.
counter.getHits(301);
Follow up:
What if the number of hits per second could be very large? Does your design scale?
public class HitCounter {
Queue<Integer> queue; /** Initialize your data structure here. */
public HitCounter() {
queue = new LinkedList<Integer>();
} /** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int timestamp) {
while (!queue.isEmpty() && queue.peek().intValue()+<=timestamp) {
queue.poll();
}
queue.offer(timestamp);
} /** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
public int getHits(int timestamp) {
while (!queue.isEmpty() && queue.peek().intValue()+<=timestamp) {
queue.poll();
}
return queue.size();
}
} /**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.hit(timestamp);
* int param_2 = obj.getHits(timestamp);
*/

Design Hit Counter的更多相关文章

  1. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  2. LeetCode Design Hit Counter

    原题链接在这里:https://leetcode.com/problems/design-hit-counter/. 题目: Design a hit counter which counts the ...

  3. LeetCode 362. Design Hit Counter

    原题链接在这里:https://leetcode.com/problems/design-hit-counter/description/ 题目: Design a hit counter which ...

  4. [LeetCode] 362. Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  5. [LC] 362. Design Hit Counter

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  6. 【LeetCode】362. Design Hit Counter 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...

  7. 362. Design Hit Counter

    这个傻逼题..我没弄明白 you may assume that calls are being made to the system in chronological order (ie, the ...

  8. AN ESAY HIT COUNTER

    <?php $counts = ("hitcounter.txt"); $hits = file($counts); $hits[0] ++; $fp = fopen($co ...

  9. 【LeetCode】设计题 design(共38题)

    链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...

随机推荐

  1. python录音程序

    # -*- coding: utf-8 -*- # @Time : 18-10-16 下午12:20 # @Author : Felix Wang import pyaudio import nump ...

  2. ImageSharp跨平台图片处理

    添加nuget引用 SixLabors.ImageSharp 和SixLabors.ImageSharp.Drawing 暂时只实现了缩略图..<pre>using SixLabors.I ...

  3. MySQL优化相关参数--先做个记录,以后可能用得到

    innodb_io_capacity:可设置的磁盘IO性能参数,越高代表当前mysql的IO性能更好,可用做决策刷脏页速度的参数: innodb_flush_neighbors:刷脏页是否开启连坐机制 ...

  4. Java学习回顾总结

    java-01初识Java见上一篇 Java-02 1.命名规范与规范: 标识符命名规则:首字母为字母|下划线|$ 其余部分数字|字母|下划线|$ 命名规范: 变量属性方法命名规范:第一个单词首字母小 ...

  5. LeetCode 41. 缺失的第一个正数(First Missing Positive)

    题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8 ...

  6. pycharm中模块不能导入的问题

    在pycharm中发现模块老是导入不成功 只能以这样的映射的方式 现在才知道: 模块的标志符可以由字母.数字.下划线组成,但是, 不能以数字开头,如果在给python文件起名时,以数字开头是无法在py ...

  7. redis的服务器信息状态信息查看

    Redis的服务器信息状态信息查看 Redis的提供了一个信息命令查看Redis的服务器的信息,类似的Linux提供一个顶级命令查看系统的信息 redis-cli info # Server #服务器 ...

  8. CentOS 7 应用

    为方便以下CentOS7简称C7,CentOS6简称C6 优化 1.安装常用功能 yum -y install bash-completion lrzsz telnet tree vim wget n ...

  9. spring cloud之docker微服务客户端注册eureka问题

    正常我们起一个微服务注册到eureka他的实例id是默认这样的主机名称:服务名称:服务端口号, 如果配置eureka.instance.prefer-ip-address=true则实例id为主机Ip ...

  10. 一百二十一:CMS系统之注册后跳转到上一个页面

    实现功能,访问测试页面的时候,跳转到注册页面,注册成功后跳转到测试页面 使用参数:若是从其他地址跳转过来时,头部信息中会携带参数referrer,此参数为从从哪个地址跳转到当前地址的,若是直接从浏览器 ...