[LeetCode] 362. Design Hit Counter

Hit the counter. Design a tap counter so that it can count the number of taps in the past 5 minutes. Each function receives a timestamp parameter (in seconds). You can assume that the earliest timestamp starts at 1 and calls the system in chronological order (that is, the timestamp is monotonically increasing). At the same moment there may be multiple knocks. example,

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); 

Two ideas.

1. Use a queue to record the timestamp of each tap. When trying to access the getHits function, check whether the difference between the timestamp of the element at the head of the queue and the current timestamp is greater than 300. If it is greater, it means that it is invalid. For elements with a difference of more than 300 from the current timestamp, the size of the queue is the number of taps in the past five minutes.

2. Use two arrays times and hits to record the time and number of hits respectively. When tapping, determine whether the timestamp is outside 300 seconds (by% 300). If it is greater than 300 seconds, the number of hits in the same position needs to be reset to 1; if it is less than 300 seconds, it is directly in the same position of hits ++ , Indicating that this is still a tap within five minutes, while recording the latest timestamp in the times array. When counting the number of taps, it traverses the hits array. If hits [i] is not 0, it is judged whether the difference between times and timestamp at the current i position is greater than 300. If it is greater, it cannot be added to the result set.

Java implementation

 1 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 /**
37  * Your HitCounter object will be instantiated and called as such:
38  * HitCounter obj = new HitCounter();
39  * obj.hit(timestamp);
40  * int param_2 = obj.getHits(timestamp);
41  */

 

Guess you like

Origin www.cnblogs.com/aaronliu1991/p/12695473.html