zoukankan      html  css  js  c++  java
  • leetcode362

    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.

    1.普通情况:Queue. 在每一秒数据不多时。
    hit:q.push(time)
    Get: 对q的peek一个个看过去,如果超时了就poll掉,最后hit数就是q.size()

    2.follow up: Array[300]. 在每一秒数据很多的情况,scale。
    Hit: %后存入。如果那个位置不是当前时间的,就用1取代,如果是当前时间,就加上去。
    Get: 统计所有有效时间的部分。O(300)稳定下来了。不是很小,但这样也总比一秒来5000条那你get一次要删5000条的O(5000)好啊。

    实现1. Queue:

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

    实现2. Array. scale:

    class HitCounter {
        
        private int[] cnt;
        private int[] time;
        /** Initialize your data structure here. */
        public HitCounter() {
            cnt = new int[300];
            time = new int[300];
        }
        
        /** Record a hit.
            @param timestamp - The current timestamp (in seconds granularity). */
        public void hit(int timestamp) {
            if (time[timestamp % 300] == timestamp) {
                cnt[timestamp % 300]++;
            } else {
                time[timestamp % 300] = timestamp;
                cnt[timestamp % 300] = 1;
            }
        }
        
        /** Return the number of hits in the past 5 minutes.
            @param timestamp - The current timestamp (in seconds granularity). */
        public int getHits(int timestamp) {
            int ans = 0;
            for (int i = 0; i < 300; i++) {
                if (time[i] + 300 > timestamp) {
                    ans += cnt[i];
                }
            }
            return ans;
        }
    }
    
    /**
     * Your HitCounter object will be instantiated and called as such:
     * HitCounter obj = new HitCounter();
     * obj.hit(timestamp);
     * int param_2 = obj.getHits(timestamp);
     */
  • 相关阅读:
    17-7-20-electron中主进程和渲染进程区别与通信
    17-7-19-书写规范和任务的延续性
    17-7-19----起
    一年没回来了
    django-BBS(2)
    django-BBS(1)
    nmap使用教程
    利用谷歌黑客语法挖掘漏洞
    PHP代码审计之XSS操作
    PHP安装文件的审计
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/9668095.html
Copyright © 2011-2022 走看看