[LC] 362. Design Hit Counter

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.

class HitCounter {

    Queue<Integer> queue;
    /** Initialize your data structure here. */
    public HitCounter() {
        queue = new LinkedList<>();
    }

    /** Record a hit.
        @param timestamp - The current timestamp (in seconds granularity). */
    public void hit(int timestamp) {
        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() && timestamp - queue.peek() >= 300) {
            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);
 */

原文地址:https://www.cnblogs.com/xuanlu/p/12010180.html

时间: 2024-07-31 22:24:03

[LC] 362. Design Hit Counter的相关文章

Leetcode 362: Design Hit Counter

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 times

Dropbox Interview – Design Hit Counter

Dropbox Interview – Design Hit Counter It starts with a simple question – if you are building a website, how do you count the number of visitors for the past 1 minute? "Design hit counter" problem has recently been asked by many companies includ

Design Hit Counter

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 times

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

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 times

LeetCode &quot;Design Hit Counter&quot;

Hits come and go - so we use queue. Nothing special. class HitCounter { queue<int> q; public: /** Initialize your data structure here. */ HitCounter() { } /** Record a hit. @param timestamp - The current timestamp (in seconds granularity). */ void h

AN ESAY HIT COUNTER

1 <?php 2 $counts = ("hitcounter.txt"); 3 $hits = file($counts); 4 $hits[0] ++; 5 $fp = fopen($counts , "w"); 6 fputs($fp , "$hits[0]"); 7 fclose($fp); 8 echo "<h2>now,the hit count is:</h2>"."&l

LC 1206. Design Skiplist

link class Skiplist { public: struct Node{ int key; Node* down; Node* next; Node(int k, Node* d, Node *n){ key=k; down=d; next=n; } }; Node* head; Skiplist() { head=new Node(-1,NULL,NULL); } bool search(int target) { Node* p=head; while(p){ Node* ne=

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.