Ctrl K

Idle Servers in a Time Window

Medium
A cluster has n servers numbered 1 to n. The log logs records traffic: logs[i] = [server, time] means server server received a request at time time. You are also given a window length window and a list of moments moments. For each moment q, count the servers that received no requests during the time interval [q - window, q] (both ends inclusive). If q - window is below 1, the interval simply starts before the earliest possible request. Return the answers in the same order as moments.
Example 1
n=3, logs=[[1, 2], [3, 7], [1, 9]], window=3, moments=[5, 9] → [2, 1]

Window [2, 5] contains only server 1's request at time 2, so servers 2 and 3 are idle. Window [6, 9] contains requests to servers 3 and 1, so only server 2 is idle. The answer is [2, 1].

Example 2
n=2, logs=[[2, 10], [2, 11]], window=4, moments=[20, 12] → [2, 1]

Window [16, 20] has no requests, so both servers are idle. Window [8, 12] has server 2 busy, so only server 1 is idle. Answers follow the query order: [2, 1].

Example 3
n=1, logs=[[1, 5]], window=5, moments=[10, 11] → [0, 1]
Constraints
  • 1 ≤ n ≤ 10⁵
  • 1 ≤ len(logs) ≤ 10⁵
  • 1 ≤ server ≤ n, 1 ≤ time ≤ 10⁶
  • 1 ≤ window ≤ 10⁵
  • 1 ≤ len(moments) ≤ 10⁵
  • 1 ≤ moments[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValues One Apart EasyNarrowest Range Touching Every List HardAnagram Buckets MediumMore Than a Third of the Votes MediumMost Common Values Medium
loading Python…
Input
n=3, logs=[[1, 2], [3, 7], [1, 9]], window=3, moments=[5, 9]
Expected
[2, 1]

Run checks the first 3 tests in your browser. Submit checks all 95 tests on the server, hidden ones included.