Dodomeki
Sign in

138. Counting Recent Accesses

Medium

A system receives access registration and query operations, each associated with a time in seconds. Operation times arrive in non-decreasing order.

The possible operations are registrar, which records an access at the given time, and contar, which asks how many accesses were recorded in the last 300 seconds, including the current time. For a query at time t, count accesses recorded at times from t - 299 through t, inclusive.

Receive the operations in operacoes and their corresponding times in valores. Both lists have the same length. Return a list with one result per operation: null for each registration and the access count for each query, in the same order as the operations.

Example 1
Input: operacoes = ["registrar", "registrar", "contar", "registrar", "contar"], valores = [10, 10, 10, 20, 309]
Output: [null, null, 2, null, 3]
Explanation: The query at 10 counts both accesses recorded at that time; at 309, the accesses at 10 and 20 are still within the window.
Example 2
Input: operacoes = ["registrar", "contar", "registrar", "contar"], valores = [5, 304, 305, 305]
Output: [null, 1, null, 1]
Explanation: At 304, the access at 5 is still included; at 305, it leaves the window and the new access is counted.
Constraints
  • 0 <= operacoes.length <= 10^5
  • operacoes.length == valores.length
  • Each element of operacoes is either "registrar" or "contar".
  • 0 <= valores[i] <= 10^9
  • valores is in non-decreasing order.
DesignQueue
Caso 1
operacoes =
["registrar","registrar","contar","registrar","contar"]
valores =
[10,10,10,20,309]
Expected =
[null,null,2,null,3]
Caso 2
operacoes =
["registrar","contar","registrar","contar"]
valores =
[5,304,305,305]
Expected =
[null,1,null,1]

Run checks these 2 cases. Submit runs all 11, including hidden ones.