## Monday, August 8, 2016

### 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 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);```
http://www.cnblogs.com/grandyang/p/5605552.html

```    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();
}

private:
queue<int> q;```

X. https://discuss.leetcode.com/topic/48758/super-easy-design-o-1-hit-o-s-gethits-no-fancy-data-structure-is-needed
O(s) s is total seconds in given time interval, in this case 300.
basic ideal is using buckets. 1 bucket for every second because we only need to keep the recent hits info for 300 seconds. hit[] array is wrapped around by mod operation. Each hit bucket is associated with times[] bucket which record current time. If it is not current time, it means it is 300s or 600s... ago and need to reset to 1.
``````public class HitCounter {
private int[] times;
private int[] hits;
/** Initialize your data structure here. */
public HitCounter() {
times = new int[300];
hits = new int[300];
}

/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int timestamp) {
int index = timestamp % 300;
if (times[index] != timestamp) {
times[index] = timestamp;
hits[index] = 1;
} else {
hits[index]++;
}
}

/** Return the number of hits in the past 5 minutes.
@param timestamp - The current timestamp (in seconds granularity). */
public int getHits(int timestamp) {
int total = 0;
for (int i = 0; i < 300; i++) {
if (timestamp - times[i] < 300) {
total += hits[i];
}
}
}
}``````

``````final int  fiveMin = 300;
/** Initialize your data structure here. */
public HitCounter() {
@Override
protected boolean removeEldestEntry(Map.Entry<Integer,Integer> eldest){
return map.size() > fiveMin;
}
};
}

/** Record a hit.
@param timestamp - The current timestamp (in seconds granularity). */
public void hit(int timestamp) {
map.put(timestamp,map.getOrDefault(timestamp,0)+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 start = timestamp - fiveMin;
int sum =0;
for(int tsp:map.keySet()){
if(tsp>start)
sum += map.get(tsp);
}
return sum;
}``````
``````public class HitCounter
{
int winLo = 0, winHi = 0;

public HitCounter() {
}

public void hit(int timestamp)
{
winHi++;

if(win.size() == 0 || win.getLast()[0] != timestamp)
else
win.getLast()[1] = winHi;

ShrinkWin(timestamp);
}

public int getHits(int timestamp)
{
ShrinkWin(timestamp);
return (int)(winHi - winLo);
}

private void ShrinkWin(int timestamp)
{
while (win.size() > 0 && timestamp - win.getFirst()[0] >= 300)
{
winLo = win.getFirst()[1];
win.removeFirst();
}
}
}``````
X. https://discuss.leetcode.com/topic/48752/simple-java-solution-with-explanation
-- use too much space
In this problem, I use a queue to record the information of all the hits. Each time we call the function getHits( ), we have to delete the elements which hits beyond 5 mins (300). The result would be the length of the queue : )
``````public class HitCounter {
Queue<Integer> q = null;
/** Initialize your data structure here. */
public HitCounter() {
}

/** 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() && timestamp - q.peek() >= 300) {
q.poll();
}
return q.size();
}
}``````

X. Extended
Interview中的一道常见题。就是要求设计数据结构来返回lastSecond(), lastMinute(), lastHour()的request数量。

Circular Buffer的确是这道题的考点，但是如果call getHits()的时间距离上一次hit()的时间早就超过了5分钟，那circular buffer里的旧数据要怎么更新是个大问题。

2. 要么在circular buffer的基础上再加一个time[] 数组记录circular buffer里每个cell的时间。
[Time & Space]

space也都为O(n)
 `// circular buffer` `class` `HitCounter {`   `  ``int``[] buffer;` `  ``int``[] time;`   `  ``public` `HitCounter() {` `    ``this``.buffer = ``new` `int``[``5` `* ``60``];` `    ``this``.time = ``new` `int``[``5` `* ``60``];` `  ``}`   `  ``public` `void` `hit(``int` `timestamp) {` `    ``int` `idx = timestamp % ``300``;` `    ``if` `(timestamp == time[idx]) {` `      ``buffer[idx]++;` `    ``}` `    ``else` `{` `      ``buffer[idx] = ``1``;` `      ``time[idx] = timestamp;` `    ``}` `  ``}`   `  ``public` `int` `getHits(``int` `timestamp) {` `    ``int` `result = ``0``;` `    ``for` `(``int` `i = ``0``; i < ``300``; i++) {` `      ``if` `(timestamp - time[i] < ``300``) {` `        ``result += buffer[i];` `      ``}` `    ``}`   `    ``return` `result;` `  ``}` `}`   `// Queue + Wrapper class` `class` `HitCounter2 {`   `  ``Deque deque;` `  ``int` `hits = ``0``;`   `  ``public` `HitCounter2() {` `    ``this``.deque = ``new` `LinkedList<>();` `  ``}`   `  ``public` `void` `hit(``int` `timestamp) {` `    ``if` `(!deque.isEmpty() && timestamp == deque.getLast().time) {` `      ``deque.getLast().cnt++;` `    ``}` `    ``else` `{` `      ``deque.offerLast(``new` `Hit(timestamp));` `    ``}` `    ``hits++;` `  ``}`   `  ``public` `int` `getHits(``int` `timestamp) {` `    ``while` `(!deque.isEmpty() && timestamp - deque.getFirst().time >= ``300``) {` `      ``hits -= deque.pollFirst().cnt;` `    ``}`   `    ``return` `hits;` `  ``}`   `  ``private` `class` `Hit {` `    ``int` `time;` `    ``int` `cnt;`   `    ``public` `Hit(``int` `time) {` `      ``this``.time = time;` `      ``this``.cnt = ``1``;` `    ``}` `  ``}` `}` `http://stackoverflow.com/questions/17562089/how-to-count-number-of-requests-in-last-second-minute-and-hour`