Design Hit Counter
Problem statement
Build a counter that records website hits and reports how many happened in the last five minutes. Process the calls in order. The two calls are:
"hit": one hit happens at secondtimestamps[i]. Several hits can happen in the same second."getHits": report how many hits so far happened in the last300seconds. For a call at timet, count the hits from timet - 299through timet, both included.
A call only sees the calls before it, so a hit that comes later in the list does not count, even if it has the same timestamp.
Return an array with one number for each "getHits" call, in order. A "hit" call adds nothing to the array.
Function
hitCounter(operations: String[], timestamps: int[]) → int[]Examples
Example 1
operations = ["hit","hit","hit","getHits","hit","getHits","getHits"]timestamps = [1,2,3,4,300,300,301]return = [3,4,3]- At time
4, the window reaches back before the first second, so it holds the hits at1,2and3, and the count is3. - At time
300, the window is1through300and also holds the new hit at300, so the count is4. - At time
301, the window is2through301. The hit at1has left, so the count is3.
Example 2
operations = ["hit","hit","hit","getHits","getHits","getHits"]timestamps = [5,5,5,5,304,305]return = [3,3,0]- Three hits happen in second
5. At time5the count is3. - At time
304, the window is5through304, so all three still count. - At time
305, the window is6through305. All three leave together, so the count is0.
Constraints
1 <= operations.length <= 10000timestamps.length == operations.length- Each
operations[i]is"hit"or"getHits". 1 <= timestamps[i] <= 2 * 10^9timestampsnever decreases:timestamps[i] <= timestamps[i + 1].