1. Description
There are n servers numbered 1 through n. Each record logs[i] = [server, time] indicates that the given server received a request at that time.
Given an integer x and an array queries, count the servers with no requests in [queries[i] - x, queries[i]] for each query. Both endpoints are included.
Return the counts in the original query order.
2. Example
Example 1
Input: n = 3, logs = [[1,3],[2,6],[1,5]], x = 5, queries = [10,11]
Output: [1,2]
Explanation: In [5,10], servers 1 and 2 are active, leaving only server 3 idle. In [6,11], only server 2 is active, so two servers are idle.
Example 2
Input: n = 3, logs = [[2,4],[2,1],[1,2],[3,1]], x = 2, queries = [3,4]
Output: [0,1]
Explanation: Every server receives a request in [1,3]. In [2,4], only server 3 receives no requests.
3. Constraints
- 1 <= n <= 10$^5$
- 1 <= logs.length <= 10$^5$
- 1 <= queries.length <= 10$^5$
- logs[i].length == 2
- 1 <= logs[i][0] <= n
- 1 <= logs[i][1] <= 10$^6$
- 1 <= x <= 10$^5$
- x < queries[i] <= 10$^6$
4. Solutions
Offline Queries with Sliding Window
n = number of servers, m = logs.size(), o = queries.size()
Time complexity: O(n + m log m + o log o)
Space complexity: O(n + o + log m)
| |