Problem · Hash Table
Per-Element Hit Counter
Learn this problemProblem statement
Process a chronological batch of hit-counter commands. Each command is one of:
HIT el timestamprecords one hit of elementelattimestamp.GET el timestampreturns how many hits ofelfall in the half-open window(timestamp - 300, timestamp].TOTAL timestampreturns how many hits of any element fall in that same window.
Timestamps are positive and non-decreasing. A hit at time t is counted by a query at time q if and only if q - 300 < t <= q.
Return the results of the GET and TOTAL commands in the order they appear.
Function
processHitCounterCommands(commands: String[]) → int[]Examples
Example 1
commands = ["HIT a 1","HIT b 2","HIT a 2","GET a 2","GET b 2","TOTAL 2"]return = [2,1,3]At time 2, element a has hits at 1 and 2, element b has one hit, and the three hits together make the total 3.
Example 2
commands = ["HIT a 1","HIT a 2","HIT b 300","GET a 301","GET b 301","TOTAL 301"]return = [1,1,2]The query window at time 301 is (1, 301], so the hit of a at time 1 has expired. The remaining hits are a at 2 and b at 300.
Constraints
1 <= commands.length <= 10000.- Each command is exactly
HIT el timestamp,GET el timestamp, orTOTAL timestamp. - Every
elis a nonempty string of at most16lowercase English letters. - Timestamps are integers in
[1, 10^9]and are non-decreasing across the batch. - At least one command is
GETorTOTAL.