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);
     */
  • 相关阅读:
    struts2 spring3 整合
    SilverLight 银光 基础.net 跨网页,桌面软件体验更好,但是要这个插件
    struts 理解 action
    vb 坐标点击
    错误struts2 json There is no Action mapped for namespace
    struts 与 jquery 整合
    springmvc jquery 界面没回应
    css 相关学习
    spring + ehcache 配置
    jquery json 结合
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/9668095.html
Copyright © 2011-2022 走看看