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

题目:

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?

题解:

维护一个queue, 每次把新的timestamp加进queue里。

需要getHits时把queue首部5min之前的全部poll出去后return queue.size().

Time Complexity: hit O(1), getHits O(queue.size()).

Space: queue.size().

AC Java:

 public class HitCounter {

     /** Initialize your data structure here. */
LinkedList<Integer> que;
public HitCounter() {
que = new LinkedList<Integer>();
} /** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int timestamp) {
que.add(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(!que.isEmpty() && timestamp - que.peek() >= 300){
que.poll();
}
return que.size();
}
} /**
* Your HitCounter object will be instantiated and called as such:
* HitCounter obj = new HitCounter();
* obj.hit(timestamp);
* int param_2 = obj.getHits(timestamp);
*/

类似Logger Rate Limiter.

LeetCode 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 362. Design Hit Counter

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

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

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

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

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

  5. Design Hit Counter

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

  6. [LC] 362. Design Hit Counter

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

  7. 362. Design Hit Counter

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

  8. [LeetCode] Design TinyURL 设计精简URL地址

    Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...

  9. LeetCode Design TinyURL

    原题链接在这里:https://leetcode.com/problems/design-tinyurl/description/ 题目: How would you design a URL sho ...

随机推荐

  1. Linux虚拟机添加新硬盘的全程图解

    查看网的文章,我将在vm虚拟机LinuxRedhat中添加一个新的硬盘, 过程大致如下: 1.选择"VM"----"setting"并打开,将光标定位在hard ...

  2. 情人节那点事,Power BI告诉你

    情人节伴随着元宵节刚刚过去,Power BI团队就送给我们一份大礼,利用来自NRF(national retail foundation)和Bing搜索的数据,在Power BI中帮助我们发现在美国那 ...

  3. kkjcre1p: unable to spawn jobq slave process, slot 0, error 1089(Linux x86_64)补丁

    在shutdown immediately的时候,alert Log出现如下错误信息,并且不能正常关闭 kkjcre1p: unable to spawn jobq slave process, sl ...

  4. [MongoDB]MongoDB与JAVA结合使用CRUD

    汇总: 1. [MongoDB]安装MongoDB2. [MongoDB]Mongo基本使用:3. [MongoDB]MongoDB的优缺点及与关系型数据库的比较4. [MongoDB]MongoDB ...

  5. ffmpeg 如何音视频同步

    转自:http://blog.csdn.net/yangzhiloveyou/article/details/8832516 output_example.c 中AV同步的代码如下(我的代码有些修改) ...

  6. 国内其他的maven库

    转自:http://www.cnblogs.com/woshimrf/p/5860478.html 在oschina关来关去的烦恼下,终于受不了去寻找其他公共库了. 阿里云maven镜像 <mi ...

  7. jQuery基础知识准备

    一. 代码风格在jQuery程序中,不管是页面元素的选择.内置的功能函数,都是美元符号"$"来起始的.而这个"$"就是jQuery当中最重要且独有的对象:jQu ...

  8. PDA 收银系统PDA手持打印扫描枪 销售开单 收银 扫描打印一体机

    在零售方面也有很好的应用.如在一些高端品牌零售店,营业员可以随身导购,一站式完成了商品销售和收银,很是受消费者追捧,符合了企业对客户体验以及行业领先的追求. PDA收银系统是一款多功能可以取代专业收银 ...

  9. jquery Ajax 案例

    html <div class="data"><ul></ul></div> <div id="load" ...

  10. appium依赖pom文件注解

    <!-- appium部分 依赖--> <dependency> <groupId>io.appium</groupId> <artifactId ...