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. QT5 网络通讯

    QT5 TCP网络通讯 服务器与客户端建立连接listen() - connectToHost();  触发newPendingConnect信号 实时数据通讯write(); read();  触发 ...

  2. Java进阶知识16 Spring创建IOC容器的两种方式

    1.直接得到 IOC 容器对象 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("app ...

  3. eclipse中不能找到dubbo.xsd

    使用dubbo时遇到问题: org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'htt ...

  4. conda 激活环境失败解决办法

    https://stackoverflow.com/questions/41746137/conda-environment-is-discoverable-but-not-activateable- ...

  5. 数据结构实验之链表五:单链表的拆分(SDUT 2120)

    #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }; st ...

  6. MySql数据类型及对应存储空间

    整数型数据类型 类型名称 说明 存储需求 TINYINT 很小的正数 1个字节 SMALLINT 小正数 2个字节 MEDIUMINT 中等大小的正数 3个字节 INT(INTEGER) 普通大小的正 ...

  7. find命令计算代码行数

    [anonymous@localhost ~/lvs/ipvsadm- -regex '.*Makefile.*' -o -regex '.*\.[ch]' -exec cat {} \; | wc ...

  8. 本地访问weblogic控制台无反应,关闭linux操作系统防火墙

    有时候,我们在Linux操作系统上成功启动了weblogic,也查看了7001端口的状态是开启的.但是访问weblogic控制台没有反应,也没有报错. 使用 netstat -ano | grep 7 ...

  9. 爬虫实践——数据存储到Excel中

    在进行爬虫实践时,我已经爬取到了我需要的信息,那么最后一个问题就是如何把我所爬到的数据存储到Excel中去,这是我没有学习过的知识. 如何解决这个问题,我选择先百度查找如何解决这个问题. 百度查到的方 ...

  10. Python中调用shell

    1 简单调用shell命令 os.system(command) 在一个子shell中运行command命令, 并返回command命令执行完毕后的退出状态. 这实际上是使用C标准库函数system( ...