Leetcode 362: Design Hit Counter

本文介绍了一种高效的设计方案,用于记录并统计过去5分钟内的点击次数。通过使用固定大小的时间戳数组和点击计数数组,该方案能够快速响应实时点击,并在任意给定时间点返回过去5分钟内的总点击数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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?

 

 1 public class HitCounter {
 2     private int[] times;
 3     private int[] hits;
 4     
 5     /** Initialize your data structure here. */
 6     public HitCounter() {
 7         times = new int[300];
 8         hits = new int[300];
 9     }
10     
11     /** Record a hit.
12         @param timestamp - The current timestamp (in seconds granularity). */
13     public void Hit(int timestamp) {
14         int index = timestamp % 300;
15         if (times[index] != timestamp) {
16             times[index] = timestamp;
17             hits[index] = 1;
18         } else {
19             hits[index]++;
20         }
21     }
22     
23     /** Return the number of hits in the past 5 minutes.
24         @param timestamp - The current timestamp (in seconds granularity). */
25     public int GetHits(int timestamp) {
26         int total = 0;
27         for (int i = 0; i < 300; i++) {
28             if (timestamp - times[i] < 300) {
29                 total += hits[i];
30             }
31         }
32         return total;
33     }
34 }
35 
36 public class HitCounterSolution1 {
37 
38     // could use a queue service like SQS to scale up
39     private Queue<int> cache = new Queue<int>();
40     
41     // to scale up and ensure duralbility , this need to store in db
42     // or use redis
43     private int count = 0;
44     
45     
46     /** Record a hit.
47         @param timestamp - The current timestamp (in seconds granularity). */
48     public void Hit(int timestamp) {
49         cache.Enqueue(timestamp);
50         
51         // this guy doesn't guarantee atomic in a multithread environment
52         // we can add lock but this would produce higher latency
53         count++;
54     }
55     
56     /** Return the number of hits in the past 5 minutes.
57         @param timestamp - The current timestamp (in seconds granularity). */
58     public int GetHits(int timestamp) {
59         while (cache.Count > 0 && timestamp - cache.Peek() >= 300)
60         {
61             cache.Dequeue();
62             count--;
63         }
64         
65         return count;
66     }
67 }
68 
69 /**
70  * Your HitCounter object will be instantiated and called as such:
71  * HitCounter obj = new HitCounter();
72  * obj.Hit(timestamp);
73  * int param_2 = obj.GetHits(timestamp);
74  */

 

转载于:https://www.cnblogs.com/liangmou/p/8165948.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值