LeetCode "Design Hit Counter"

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 hit(int timestamp) {
        q.push(timestamp);
    }

    /** Return the number of hits in the past 5 minutes.
        @param timestamp - The current timestamp (in seconds granularity). */
    int getHits(int timestamp) {
        while(!q.empty() && (timestamp - q.front()) >= 300) q.pop();
        return q.size();
    }
};
时间: 2024-10-06 00:12:59

LeetCode "Design Hit Counter"的相关文章

[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

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

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

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

[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 times

[LeetCode] Design Phone Directory 设计电话目录

Design a Phone Directory which supports the following operations: get: Provide a number which is not assigned to anyone. check: Check if a number is available or not. release: Recycle or release a number. Example: // Init a phone directory containing

[LeetCode] Design Snake Game 设计贪吃蛇游戏

Design a Snake game that is played on a device with screen size = width x height. Play the game online if you are not familiar with the game. The snake is initially positioned at the top left corner (0,0) with length = 1 unit. You are given a list of

LeetCode Design Compressed String Iterator

原题链接在这里:https://leetcode.com/problems/design-compressed-string-iterator/description/ 题目: Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext. The given compressed string

[LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器

Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext. The given compressed string will be in the form of each letter followed by a positive integer representing the numbe